1. Overview

Network communication protocols are standards for communication between devices on the Internet or in a computer network. Out of all the communication protocols, two prevalent and widely used protocols are TCP and UDP.

In this tutorial, we’ll discuss the basic concept of TCP and UDP protocols. We’ll explain whether TCP and UDP sockets can work on the same port or not with examples.

2. Introduction to TCP and UDP

TCP and UDP are two standard protocols that work with the Internet Protocol (IP). Both TCP and UDP protocols are used in the transport layer of the OSI model.

TCP stands for Transmission Control Protocol is a connection-oriented protocol. Two computers make a client-server connection via handshake before transferring data packets using TCP. Additionally, TCP puts header information in data packets so that packets can be reassembled at the destination computer.

We use TCP when the reliability of the data is essential. It also ensures that the data sent is in order. Additionally, if the receiver doesn’t receive the data transmitted by a sender, TCP re-sends the packets after a specific time. Moreover, we use the checksum for error control in TCP to ensure data integrity.

User Datagram Protocol (UDP) is a connection-less protocol. It doesn’t ensure that protocol will receive data at the receiver end. UDP has lower latency in comparison to TCP. It’s faster because there’s no re-transmission mechanism in UDP.

Applications for which time is more important than reliability use the UDP protocol. As UDP is stateless, applications that require many connections like media streaming use UDP. Online games, VOIP, streaming media use UDP as time is more important for these applications than consistent data.

For a more detailed comparison, we refer to an article on the differences between TCP and UDP.

3. Network Ports

Programs in the application layer communicate with the transport layer through a port. We assign different ports to different applications so that they can connect and exchange data with any networking-enabled devices within a network using their port.

For example, port number 80 is reserved for HTTP. Ports help in multiplexing so that multiple network connections or multiple communications can take place at the same device.

It’s a 16-bit number. Hence, it can have 2^{16} values between \mathsf{0} and \mathsf{65535}. Therefore, we can have a total of \mathsf{65536} TCP ports in a computer network. Similarly, the network can contain \mathsf{65536} UDP ports as well. When we send the data from one device to another, it goes to a specific TCP or UDP port, depending upon the protocol we’re using for communication.

Ports are like multiple counters in a branch of a bank. Different customers can simultaneously perform their banking job using counters.

4. Can TCP and UDP Sockets Use the Same Port?

TCP and UDP ports are not related to each other. TCP ports are interpreted by the TCP stack, while the UDP stack interprets UDP ports. Ports are a way of multiplexing the connection so that multiple devices can connect to a node.

Hence, technically, higher-level protocols can use the same or different TCP and UDP port numbers. On the other hand, a single computer can communicate with two different services using the same port numbers for TCP and UDP simultaneously.

For example, web servers bind to TCP port 80. The applications use the same port number for TCP and UDP so that it’s easier to remember. Although two applications connecting to the same port number might do a similar task, TCP and UDP ports are different. They’re independent of each other.

Let’s consider our bank example again. Let’s assume TCP and UDP are two different banks. By convention, both TCP and UDP banks decide to make counter \mathsf{1} as a reception. Additionally, they also set counters 2 and 3 for handling cash.

Hence, both TCP and UDP bank customers can go to counter \mathsf{1} for the reception. Moreover, if they want to deposit cash, they can choose between counters 2 and 3. Although the counter numbers are the same, the banks are entirely different. Moreover, both banks are free to reassign their counters for different tasks anytime they want.

Hence, in short, TCP and UDP are not related. Higher-level protocols may use the same port number by convention to avoid confusion. 

5. An Example

Let’s talk about an example where we use the same port number for TCP and UDP sockets.

The domain name system (DNS) is a decentralized system that matches domain names to IP addresses. It also fetches other domain-related records using a domain name.

Let’s look at the port numbers used by the DNS and some of the popular protocols:

bitmap2

The DNS relies on both UDP and TCP. Moreover, the port number 53 is used by both TCP and UDP for the DNS-related tasks. Initially, the DNS was compatible with only the UDP port 53. However, the TCP port was added later to ensure reliability and connection reuse.

The DNS typically works on UDP, but if the packet size is large, it connects over TCP port \mathbf{53} and performs the required operations. Unlike UDP, which can send only small packets, TCP has no limit on the data. Hence, if there’s a failure while sending a request in UDP, the communication switches to TCP.

Zone transfer is the process of replicating a DNS server by transferring complete DNS record data to another machine. It uses the TCP port 53. Other DNS-related operations can use both UDP or TCP port 53 depending on the situation.

6. Conclusion

In this tutorial, we presented a general overview of the ports used by TCP and UDP sockets. In addition, we explained whether TCP and UDP sockets could use the same port or not with examples.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.