This article is the reading noetes for project httpd, which is writen in 1999.GitHub addr
AF_INET(address family) ==
PF_INET (protocol family);
void * memset ( void * ptr, int value, size_t num );
Set the first num bytes of the block of memory pointed by ptr to the specified value value;
htonl, htons, ntohl, ntohs – convert values between host and network byte order. And the function of each fun() is showed in their name. For instance,
htonl(uint32_t hostlong) means: convert long from host bytes order to network bytes order.
setsockopt – get and set options on sockets
#include <sys/types.h> #include <sys/socket.h> int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
SO_REUSEADDR: when this argument is used, means that if the port is busy but TCP is in
time_wait stage, then this port can be reused, if TCP in other stage, will get a error message showing this addr is in use. So it is quite useful when your sever reboot and wanna use the same port.
create an endpoint for communication and returns a file descriptor that refers to that endpoint.
#include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int socket(int domain, int type, int protocol);
#include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
When a socket is created with socket(2), it exists in a name space (address family) but has no address assigned to it. bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called “assigning a name to a socket”.
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
returns the current address to which the socket
sockfd is bound, in the buffer pointed by
int listen(int sockfd, int backlog);
listen() marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be used to accept incoming connection requests using
backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow.
Initiate a connection on a socket
#include <sys/types.h> #include <sys/socket.h> int connect(int sockfd, const struct sockaddr addr, socklen_t addrlen);
The connect() system call connects the socket referred to by the file
descriptor _sockfd_ to the address specified by _addr_. The _addrlen_ argument specifies the size of _addr_.
accepte a connection on a socket
#include <sys/types.h> #include <sys/socket.h> int accept(int sockfd, struct sockaddr addr, socklen_t addrlen);
The accept() system call is used with connection-based socket types
(**SOCK_STREAM**, **SOCK_SEQPACKET**). It extracts the first connection request on the queue of pending connections for the listening socket, _sockfd_, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state. The original socket _sockfd_ is unaffected by this call.
This function init the http service, including set up socket, bind port and listen for connections;
Handle a connection from the coneections queue.