Skip to content

Understanding TCP and UDP

Webservers typically rely upon two communication protocols Hypertext Transfer Protocol (HTTP) and Transmission Control Protocol (TCP). Both of these protocols are request-response protocols. This means that a client creates requests, and a server listens for these requests, processes them, and returns a response.

TCP is a ??session layer protocol, meaning that it operates at a lower level than HTTP. It ensures the reliable transportation of data, but doesn't articulate what information is contained within the data, effectively transferring raw bytes. HTTP is an application layer protocol, thereby providing knowledge of the information of the data being transferred.

Webservers need to listen for TCP connections. Programming languages often provide bindings for this in their standard library. In Rust this is the std::net module. In command parlance, the intialization of a TCP listener to a specific port is called binding. Binding to a port is an exclusive operation in that, once one listener is bound to a port, no other listeners can bind to it (and if they can, they shouldn't, or should remove the other one first).

Streams represent open connections between a client and a server. In networking, a connection refers to the whole process of sending a request from the client, processing and responding to the request by the server, and closing the connection (if required for the particular application).

Notes

A socket is simply a combination of an IP and a port. Combining a transport layer port number and the network layer IP address uniquely IDs a particular appliction process running on an individual host (e.g. 192.168.1.141:31001)

Port 80 is typically assigned to HTTP and 443 to HTTPS

Tips

  • Non-admins can only listen on ports higher than 1023.
Tip
  • A firewall typically identifies packets flowing between two networks with a 5-tuple (source_ip, dest_ip, source_port, dest_port, protocol). This information is extracted from the TCP headers in each packet.

... Stopped at Rust Book 20.1 ...