Introduction to TCP/IP (Part 4) - Sockets and Ports
TCP/IP Ports
Ports are used to identify processes running in the applications on a host.
Let's assume we have two applications running on one PC that require TCP/IP communications. Assume one is a web browser and the other is an email client.
Both applications send and receive packets with the same IP address, so how does the Transport layer differentiate a web browser packet from an email packet?
The answer is port numbers.
TCP/IP "Well-Known" Ports
“Well-known" ports are port numbers that have been reserved for common applications, typically server applications. The port numbers assigned to these server applications have to be known by the client’s Transport layer, so they can add the correct destination port number to messages. Clients know that servers will be listening for their requests at these reserved port numbers. This graphic shows some examples of these well-known port numbers.
The well-known port numbers are assigned by IANA which is the Internet Assigned Numbers Authority. IANA is the same group that manages the DNS Root and IP addresses.
Ephemeral or Dynamic Ports
Client-side port numbers are generated and assigned by the Transport layer. They could be any number from 1024 to 65535. These port numbers are typically allocated for short-term use and are referred to as “Ephemeral or Dynamic Ports”.
Sockets
Sockets Defined
A socket is a software concept for a connection. Sockets enable applications to connect to a Transmission Control Protocol/Internet Protocol (TCP/IP) network.
An application running on a host creates a socket or doorway to connect with an application on another host. Messages pass through this socket or doorway.
Sockets enable virtual TCP or UDP communication channels between hosts.
When an application starts on a host, a port number is assigned to a process or a function running in it. When that application wants to communicate with another host, (go to a website for example) a socket is created.
This example shows three applications requiring three TCP communication channels: Two channels for each of the two web browsers acting as HTTP clients, and one for the email application acting as an SMTP client.
Sockets are physically implemented as transmit (TX) & receive (RX) memory buffers.
When an application wants to transmit a message, a process writes to the socket’s transmit buffer. This same process periodically checks the socket’s receive buffer for messages being sent by the host on the other end of the virtual connection.
The Transport layer delivers messages to the application by writing them to the socket’s receive buffer. The Transport layer also periodically polls the socket’s transmit buffer to determine if there are messages to send.
Example: Established Socket
A socket is created by an application running in a host. The application assigns a transport protocol (TCP or UDP) and source and destination addresses to the socket. It identifies sockets by assigning numbers to them.
Note the web server has two sockets opened: one for each web page it is serving. These sockets are differentiated by the destination port numbers.
This graphic shows a virtual TCP connection between a client and server. Note the socket numbers are not the same on both sides of the channel. Hosts create, close and number their own sockets.
Example: Use Sockets to Create a TCP Connection
The following steps describe a TCP connection process using sockets.
- Server Creates Socket and Listens
- Client Creates a Socket and Connects
- Transport Layer Delivers Message to Server
- Server Creates Socket & Process
- Transport Layer Delivers Message to Client
- Sockets Closed
Server Creates Socket and Listens
A web server creates a socket dedicated to listening for client requests. After the socket exists, the server goes into “listening” mode and waits for a client's request. It periodically checks for messages received in this socket.
This type of socket is referred to as a connectionless socket. A connectionless socket is used to establish a TCP connection with the HTTP server. There is no destination IP address or port number defined for this type of socket.
Client Creates a Socket and Connects
When a client wants to download a web page it creates a socket and then sends the web page download request to the socket.
Transport Layer Delivers Message to Server
The client’s Transport layer periodically checks its transmit buffers to determine if a message needs to be sent. When a message is found it is forwarded to the destination address.
Server Creates Socket and Process
When the server receives the client’s request, it creates a new dedicated socket and process. It then creates a message for the client and sends it to the socket.
Note this socket uses the client’s destination IP address and port number. This virtual TCP connection is now referred to as “established”.
Transport Layer Delivers Message to Client
The server’s Transport layer periodically checks its transmit buffers to determine if a message needs to be sent.
When a message is found it is forwarded to the destination address.
Sockets Closed
After the client receives the web page it requested, it sends an acknowledge to the server and then closes its socket.
The server receives the client’s acknowledge then closes its socket.
Berkeley Sockets
No class on sockets can be completed without mentioning Berkeley sockets. Berkeley sockets is an industry-standard Application Programming Interface (API) to create and use sockets. It was initially used as an API for the Unix operating system and was later adopted by TCP/IP.
Berkeley defines 18 standard function names for this purpose. This graphic shows a few examples.
The socket() function creates a socket on the host.
The bind() function is typically used on the server side and assigns a socket to its local IP address and port number. Connect() is typically used on the client side. It creates a socket and also attempts to establish a TCP or UDP connection with a server.
Send(), recv(), write(),and read() are used to send and receive the messages to and from the socket.