In the TCP/IP protocol suite, there are dozens of different protocols, but the most important ones are TCP and IP. IP provides addressing, datagram routing, and other functions for connecting one machine to another, while TCP is responsible for managing connections and provides reliable data transport between processes on two machines. IP is located in the network layer (layer 3) in the Open Systems Interconnection (OSI) model, whereas TCP is located in the transport layer (layer 4) of OSI.
Besides TCP, the other key protocol in the transport layer is UDP. You may be asking what the differences between these two protocols are.
In brief, TCP has the following characteristics:
- This is a connection-oriented protocol: Before TCP can be used for sending data, the client and the server that want to communicate must establish a TCP connection using a three-way handshake mechanism, as follows:
- The client initiates the connection by sending a packet containing a SYN (synchronize) flag to the server. The client also sends the Initial Sequence Number (ISN) in the sequence number field of the SYN segment. This ISN is chosen randomly.
- The server replies with its own SYN segment containing its ISN. The server acknowledges the client's SYN by sending an ACK (acknowledgment) flag containing the client ISN + 1 value.
- The client acknowledges the server by sending an ACK flag containing the server ISN + 1. At this point, the client and the server can exchange data.
- To terminate the connection, the TCP must follow this mechanism:
- The client sends a packet containing a FIN (finish) flag set.
- The server sends an ACK (acknowledgment) packet to inform the client that the server has received the FIN packet.
- After the application server is ready to close, the server sends a FIN packet.
- The client then sends the ACK packet to acknowledge receiving the server's FIN packet. In a normal case, each side (client or server) can terminate its end of the communication independently by sending the FIN packet.
- This is a reliable protocol: TCP uses a sequence number and an acknowledgment to identify packet data. The receiver sends an acknowledgment when it has received the packet. When a packet is lost, TCP will automatically retransmit it if it hasn't received any acknowledgment from the receiver. If the packets arrive out of order, TCP will reorder them before submitting them to the application.
- Applications that need to transfer files or important data use a TCP, such as Hypertext Transport Protocol (HTTP) and File Transfer Protocol (FTP).
UDP has opposing characteristics to TCP, which are as follows:
- This is a connectionless protocol. To send data, the client and the server don't need to establish a UDP connection first.
- It will do its best to send a packet to the destination, but if a packet is lost, UDP will not automatically resend it. It is up to the application to retransmit the packet.
Applications that can bear the loss of some packets, such as video streaming and other multimedia applications, use UDP. The other well-known applications that use UDP are Domain Name System (DNS), Dynamic Host Configuration Protocol (DHCP), and Simple Network Management Protocol (SNMP).
For applications to be able to communicate correctly, the transport layer uses addressing, called ports. A software process listens on a particular port number on the server side, and the client machine sends data to that server port to be processed by the server application. The port numbers have a 16-bit address, and the number can range from 0 to 65,535. To avoid a chaotic usage of port numbers, there are universal agreements on port number ranges, as follows:
- Well-known port numbers (0 to 1,023): Port numbers in this range are reserved port numbers and are usually used by the server processes that are run by a system administrator or privileged user. Examples of the port numbers used by an application server are SSH (port 22), and HTTP (port 80), HTTPS (port 443).
- Registered port numbers (1,024 to 49,151): Users can send a request to the Internet Assigned Number Authority (IANA) to reserve one of these port numbers for their client-server application.
- Private or dynamic port numbers (49,152 to 65,535): Anyone can use the port numbers in this range without registering them with the IANA.
After discussing the differences between TCP and UDP in brief, let's describe TCP and UDP message formats.