Network Programming

It is important to realize that clients and servers are processes and not machines, or hosts as they are often called in this context.

Networks

The Global Internet

IP Addresses

TCP/IP defines a uniform network byte order (big-endian byte order) for any integer data item, such as an IP address, that is carried across the network in a packet header. Addresses in IP address structures are always stored in (big-endian) network byte order, even if the host byte order is little-endian.

#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);

// returns value in network byte order

uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(unit16_t netshort);

// returns value in host byte order

Application programs can convert back and forth between IP addresses and dotted-decimal strings using the functions inet_pton and inet_ntop.

AE_INET for IPv4, AE_INET6 for IPv6.

Internet Domain Names

Screenshot 2023-05-03 at 12.09.24 PM

Internet Connections

Full duplex: the data can flow in both directions

A socket is an end point of a connection. Each socket is identified by the socket address address:port.

The port in the server’s socket address is typically some well-known port that is permanently associated with the service. The mapping between well-known names (smtp, http) and well-known ports (25, 80) is contained in a file called /etc/services.

By default, HTTP requests use port 80 for non-secure (HTTP) connections and port 443 for secure (HTTPS) connections. However, if the server is running on a different port, you can specify the port number in the request URL, like http://fduhole.com:8080/.

A socket tuple: (cliaddr:cliport, servaddr:servport)

Web Servers

Web Content

MIME type
Description

text/html

HTML page

text/plain

Unformatted text

application/postscript

Postscript document

image/gif

Binary image encoded in GIF format

image/png

Binary image encoded in PNG format

image/jpeg

Binary image encoded in JPEG format

Web servers provide content to clients in two different ways:

  • Fetch a disk file return its content to the client. Serving static content

  • Run a executable file and return its output to the client. Serving dynamic content

The minimal URL suffix is the ‘/’ character, which all servers expand to some default home page such as /index.html.

HTTP Transactions

A request line has the form model URI version

The GET method instructs the server to generate and return the content identified by the URI. The URI is the suffix of the corresponding URL that includes the filename and optional arguments.

The Host header is required in HTTP/1.1 requests, but not in HTTP/1.0 requests. The Host header is used by proxy caches, which sometimes serve as intermediaries between a browser and the origin server that manages the requested file. Multiple proxies can exist between a client and an origin server in a so-called proxy chain. The data in the Host header, which identifies the domain name of the origin server, allow a proxy in the middle of a proxy chain to determine if it might have a locally cached copy of the requested content.

A response line has the form version status-code status-message

Sockets With Protection

We folk a child process to server the client in the server.

Screenshot 2023-05-27 at 7.27.47 AM

Concurrency

Use threads, without Protection:

Screenshot 2023-05-27 at 7.40.23 AM

Problem with this: unbounded threads. When the website becomes too popular, there's throughput sink.

Instead, we allocate a bounded "pool" of worker threads, representing the maximum level of multiprogramming.

Last updated