CS:APP CH11 Network Programming
CMU 2015 Fall: 15-213 Lecture 21
CMU 2015 Fall: 15-213 Lecture 22
CS:APP Code Examples
Key words: processes, signals, byte ordering, memory mapping, dynamic storage allocation.
Be careful, this book will not discuss IPv6 in any detail and will focus exclusively on the concepts behind IPv4.
11.2 Networks
To a host, a netork is just another I/O device that serves as a source and sink for data:

Data received from the network are copied from the adapter across the I/O and memory buses into memory, typically by a DMA transfer. Similarly, data can also be copied from memory to the network.
11.3 The Global IP Internet
Because Internet hosts can have different host byte orders, TCP/IP defines a uniform network byte order (big-endian) for any integer data item, such as an IP address, that is carried across the network in a packet header.
Unix provides the following functions for coverting between network and host byte order:
port
- Ephemeral: Assigned automaitcally by the client kernel when the client makes a connection request.
- Well-known: Associated with some service provided by a server. The mapping between well-known names and well-known ports is contained in a file called /etc/services.
11.4 The Sockets Interface

Socket address structure:
The problem faced by the designeds of the sockets interface was how to define these functions to accept any kind of socket address structure. Today, we would use the generic void * pointer, which did not exist in C at that time. Their solution was to define sockets functions to expect a pointer to a generic sockaddr structure and then require applications to cast any pointers to protocol-specific structures to this generic structure.
socket()
- Create a socket descriptor.
- AF_INET indicates that we are using 32-bit IP addresses.
- SOCK_STREAM indicates that the socket will be an end point for a connection.
- See socket(2).
- The best practice is to use getaddrinfo() to generate these parameters automically, so that the code is protocol-independent. (11.4.8)
- Return nonnegative descriptor if OK, -1 on error.
connect()
- A client establishes a connection with a server by calling the connect().
- Return: 0 if OK, -1 on error.
bind()
- Ask the kernel to associate the server's socket address in addr with the socket descriptor sockfd.
- The best practice is to use getaddrinfo() to supply the arguments. (11.4.8)
- Return: 0 if OK, -1 on error.
listen()
- Convert sockfd from an active socket to a listening socket that can accept connection requests from clients.
- The backlog argument is a hint about the number of outstanding connection requests that the kernel should queue up before it starts to refuse requests.
- Return: 0 if OK, -1 on error.
accept()
- Wait for a connection request from a client to arrive on the listening descriptor listenfd, then fills in the client's socket address in addr, and returns a connected descriptor that can be used to communicate with the client usong Unix I/O functions.
- Return nonnegative descriptor if OK, -1 on error.
getaddrinfo()
- Convert string representations of host names, host addresses, service names, and port numbers into socket address structures.
- Replace obsolete gethostbyname(), getservbyname().
- It is reentrant and works with any protocol.

The addrinfo structure:
11.5 Web Servers