HTTP Server in C
C, HTTP, Sockets
This article describes a simple http server socket in Linux.
Server Socket
The basic procedure:
- Create socket with
socket()
call bind()
this to an IP and port where it canlisten()
for connections, thenaccept()
connection andsend()
orreceive()
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.
Simple Example
Protocol Family
When setting up a socket using socket()
the following protocols are available:
Name | Purpose | Man page |
---|---|---|
AF_UNIX , AF_LOCAL |
Local communication | man unix |
AF_INET |
IPv4 Internet protocols | man ip |
AF_INET6 |
IPv6 Internet protocols | man ipv6 |
AF_IPX |
IPX - Novell protocols | |
AF_NETLINK |
Kernel user interface device | man netlink |
AF_X25 |
ITU-T X.25 / ISO-8208 protocol | man x25 |
AF_AX25 |
Amateur radio AX.25 protocol | |
AF_ATMPVC |
Access to raw ATM PVCs | |
AF_APPLETALK |
AppleTalk | man ddp |
AF_PACKET |
Low level packet interface | man packet |
AF_ALG |
Interface to kernel crypto API |
You can acces this list by running man socket
.
Socket Type
The socket has the indicated type, which specifies the communication semantics. Currently defined types are:
Type | Description |
---|---|
SOCK_STREAM |
Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported. Used for TCP protocol. |
SOCK_DGRAM |
Supports datagrams - connectionless, unreliable messages of a fixed maximum length. Used for UDP protocol. |
SOCK_SEQPACKET |
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. |
SOCK_RAW |
Provides raw network protocol access. |
SOCK_RDM |
Provides a reliable datagram layer that does not guarantee ordering. |
SOCK_PACKET |
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”.
Connecting
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.
Using getnameinfo
Used in the report()
function
References
comments powered by Disqus