Appendix A. More HTTP Server Examples
This appendix expands the contents of “Beating the C10k Problem” by providing more examples of HTTP servers. These examples are not essential to understand Chapter 5, but you might find them interesting. Also, some of these examples are included in the benchmarks.
fork() Procedure in C Language
We will try to implement a concurrent HTTP server using C.
If you are familiar with C, you will find the following program fairly straightforward.
Otherwise, do not worry, you are not obligated to understand all of the details, just the overall idea.
Invoking fork()
makes a copy of the current process, so that suddenly two processes appear in the operating system: the original one (parent) and a child.
This second process has the exact same variables and state, the only difference is the result value of fork()
:
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
int
main
(
int
argc
,
char
*
argv
[])
{
signal
(
SIGCHLD
,
SIG_IGN
);
struct
sockaddr_in
serv_addr
;
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
serv_addr
.
sin_port
=
htons
(
8080
);
int
server_socket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
server_socket
<
0
)
{
perror
(
"socket"
);
exit
(
1
);
}
if
(
bind
(
server_socket
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
perror
(
"bind"
);
exit
(
1
);
}
listen
(
server_socket
,
100
);
struct
sockaddr_in
cli_addr
;
socklen_t
Get Reactive Programming with RxJava now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.