00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00036 #include <stdio.h>
00037
00038 #include <net/in.h>
00039 #include <net/inet.h>
00040 #include <net/socket.h>
00041
00042 #include <str.h>
00043
00044 #define PORT_NUMBER 8080
00045
00047 #define BUFFER_SIZE 1024
00048 static char buf[BUFFER_SIZE];
00049
00051 static const char *response_msg =
00052 "HTTP/1.0 200 OK\r\n"
00053 "\r\n"
00054 "<h1>Hello from HelenOS!</h1>\r\n";
00055
00056 int main(int argc, char *argv[])
00057 {
00058 struct sockaddr_in addr;
00059 struct sockaddr_in raddr;
00060
00061 socklen_t raddr_len;
00062
00063 int listen_sd, conn_sd;
00064 int rc;
00065
00066 size_t response_size;
00067
00068 addr.sin_family = AF_INET;
00069 addr.sin_port = htons(PORT_NUMBER);
00070
00071 rc = inet_pton(AF_INET, "127.0.0.1", (void *) &addr.sin_addr.s_addr);
00072 if (rc != EOK) {
00073 printf("Error parsing network address.\n");
00074 return 1;
00075 }
00076
00077 printf("Creating socket.\n");
00078
00079 listen_sd = socket(PF_INET, SOCK_STREAM, 0);
00080 if (listen_sd < 0) {
00081 printf("Error creating listening socket.\n");
00082 return 1;
00083 }
00084
00085 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));
00086 if (rc != EOK) {
00087 printf("Error binding socket.\n");
00088 return 1;
00089 }
00090
00091 rc = listen(listen_sd, 1);
00092 if (rc != EOK) {
00093 printf("Error calling listen() (%d).\n", rc);
00094 return 1;
00095 }
00096
00097 response_size = str_size(response_msg);
00098
00099 printf("Listening for connections at port number %u.\n", PORT_NUMBER);
00100 while (true) {
00101 raddr_len = sizeof(raddr);
00102 conn_sd = accept(listen_sd, (struct sockaddr *) &raddr,
00103 &raddr_len);
00104
00105 if (conn_sd < 0) {
00106 printf("accept() failed.\n");
00107 return 1;
00108 }
00109
00110 printf("Accepted connection, sd=%d.\n", conn_sd);
00111
00112 printf("Wait for client request\n");
00113
00114
00115 rc = recv(conn_sd, buf, BUFFER_SIZE, 0);
00116 if (rc < 0) {
00117 printf("recv() failed\n");
00118 return 1;
00119 }
00120
00121
00122 printf("Send response...\n");
00123 rc = send(conn_sd, (void *) response_msg, response_size, 0);
00124 if (rc < 0) {
00125 printf("send() failed.\n");
00126 return 1;
00127 }
00128
00129 rc = closesocket(conn_sd);
00130 if (rc != EOK) {
00131 printf("Error closing connection socket: %d\n", rc);
00132 return 1;
00133 }
00134
00135 printf("Closed connection.\n");
00136 }
00137
00138
00139 return 0;
00140 }
00141