Before entertaining the exciting parts of the HTTP server, such as message parsing, let's get the basics out of the way. Our HTTP server, like all servers, needs to create a listening socket to accept new connections. We define a function, create_socket()
, for this purpose. This function begins by using getaddrinfo()
to find the listening address:
/*web_server.c except*/ SOCKET create_socket(const char* host, const char *port) { printf("Configuring local address...\n"); struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; struct addrinfo *bind_address; getaddrinfo(host, port, &hints, &bind_address);
create_socket()
then continues with creating a socket using socket()
, binding that socket to the listening address with bind()
, and having the socket enter a listening state with listen()
. The following code calls these functions while...