static void process_request int sock char buf 1024 read sock buf sizeo

 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
static void process_request(int sock)
{
char buf[1024];
read(sock, buf, sizeof(buf));
write(sock, response, strlen(response));
close(sock);
exit(0);
}
static void server_loop(int server_socket)
{
int fd;
struct sockaddr_in remote_addr;
pid_t pid;
if (listen(server_socket, 5) < 0)
exit(1);
signal(SIGCHLD, SIG_IGN);
while (1)
{
socklen_t addr_sz = sizeof(remote_addr);
fd = accept(server_socket, (struct sockaddr*)&remote_addr, &addr_sz);
if (fd > 0)
{
pid = fork();
if (pid < 0)
{
close(fd);
continue;
}
if (pid > 0)
{
continue;
}
close(server_socket);
process_request(fd);
}
}
}