-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
55 lines (44 loc) · 953 Bytes
/
Copy pathmain.c
File metadata and controls
55 lines (44 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "common.h"
#include "server.h"
void handleRequest(int sockfd);
void handleError(char *errorMsg);
void exitHandler();
int main(int argc, char** argv)
{
// ignore signals that I don't care about...
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, exitHandler);
signal(SIGQUIT, exitHandler);
signal(SIGTERM, exitHandler);
HttpServer *server = ServerInit(8000, handleRequest, handleError, exitHandler);
ServerStart(server);
return 0;
}
void exitHandler()
{
printf("Shutting down...\n");
exit(0);
}
void handleRequest(int sockfd)
{
size_t len = 1024;
char buffer[len];
ssize_t n;
bzero(buffer, len);
n = read(sockfd, buffer, len-1);
if (n < 0)
{
handleError("Error reading from socket.");
}
char *response = "Hello world!";
n = write(sockfd, response, strlen(response));
if (n < 0)
{
handleError("Error writing to socket.");
}
}
void handleError(char *errorMsg)
{
printf("%s\n", errorMsg);
}