Simple http server socket in Linux. This is an educational work in progress, and these are my notes.
The basic procedure:
Create socket with socket() call
bind() this to an IP and port where it can
listen() for connections, then
accept() connection and send() or receive() data to/from connected sockets
Note that if struct sockaddr_in serverAddress.sin_addr.s_addr is set to INADDR_ANY the socket is bound
to all local interfaces. INADDR_ANY is a constant set to zero, defined in netinet/in.h. This will correspond to an IP address of 0.0.0.0 in the standard IPv4 notation. Note that htonl(INADDR_LOOPBACK) and inet_addr("127.0.0.1") are functionally equivalent.
When setting up a socket using socket() the following protocols are available:
IPv4 Internet protocols
IPv6 Internet protocols
IPX - Novell protocols
Kernel user interface device
ITU-T X.25 / ISO-8208 protocol
Amateur radio AX.25 protocol
Access to raw ATM PVCs
Low level packet interface
Interface to kernel crypto API
You can acces this list by running man socket.
The socket has the indicated type, which specifies the communication semantics. Currently defined types are:
Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported. Used for TCP protocol.
Supports datagrams - connectionless, unreliable messages of a fixed maximum length. Used for UDP protocol.
Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each input system call.
Provides raw network protocol access.
Provides a reliable datagram layer that does not guarantee ordering.
Obsolete and should not be used in new programs; see packet(7).
Constructing a Local Address Structure
If serverAddress.sin_addr.s_addr is set to INADDR_ANY the socket is bound to all local interfaces. INADDR_ANY is a constant set to zero, defined in netinet/in.h. This will correspond to an IP address of 0.0.0.0 in the standard IPv4 notation. Note that htonl(INADDR_LOOPBACK) and inet_addr("127.0.0.1") are functionally equivalent.
Bind socket to local address
When a socket is created with socket(), it exists in a name space (address family) but has no address assigned to it. bind() assigns the address specified by serverAddress to the socket referred to by the file descriptor serverSocket. serverAddressLength specifies the size, in bytes, of the address structure pointed to by serverAddress.
This operation is known as “assigning a name to a socket”.
The programme runs an infinite loop in which we wait and create a connected socket if a connection is pending.
The accept() function gets the first connection request on the queue of pending connections for the listening socket (in this case denoted by serverSocket). It then creates a new connected socket and returns a file descriptor referring to this socket. The newly created socket is NOT in a listening state. The original socket (serverSocket) is unaffected by this call.