What is UDP/TCP?

Iremsu Koc
5 min readSep 8, 2023

--

Hello everyone! In this article, we will explain what TCP and UDP protocols are, how they send data, and what their structure is.

What is the UDP/TCP?

TCP ( Transmission Control Protocol) and UDP (User Datagram Protocol) are the most widely used protocols that enable data to be transported over the internet, across the country or even the world.

1.1 UDP

UDP (User Datagram Protocol) is a communication protocol that allows data to be sent quickly and with low latency, and does not provide reliability and connection management.

1.1.1 Frame/Packet Structure

UDP’s frame/packet structure is quite simple and includes the following features:

1. Header: The header of the UDP packet has a fixed size and is usually 8 bytes long. The header contains two main fields:

  • a. Source Port Number: Indicates which port number the sending device uses. This field is 16 bits long.
  • b. Destination Port Number: Indicates which port number the target device to which the data will be sent uses. This field is also 16 bits long.

2. Length: This field specifies the total packet length within the header in bytes. This collects the length of the UDP header and payload.

3. Checksum: Used to check the data integrity of the transmitted UDP packet.

Note, however, that UDP does not provide reliability; therefore, there is no retransmission or processing capability to detect and correct erroneous data packets.

4. Data Payload: This section contains the actual data and carries the information to be transmitted. The payload length includes remaining bytes from the header and other fields.

Fig.1 — UDP Frame Structure

1.1.2 Sending Data

Sending data with UDP includes the following steps:

1. Creating Sockets: To start sending data via UDP, you need to create a socket in a programming language such as Python. A socket represents the communication point through which data will be sent and received.

The desired socket type is specified with two parameters to the socket() system call. AF_INET must be used for Internet Protocol, and SOCK_DGRAM (datagram socket) must be used for UDP connection. If the returned value is negative, it indicates that an error occurred. If there are no problems, this system call returns the socket identifier.

import socket

# Create socket for IPv4 and UDP
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

2. Determining the Target Computer and Port: You need to specify the IP address and destination port number of the destination computer to which you will send the data. This specifies where to send the data.

target_ip = "192.168.1.100"
target_port = 12345

You can determine the port number of your computer or server that indicates how to access a particular application or service by using the following methods:

  • Using Official Port Numbers: The Internet Assigned Numbers Authority (IANA) defines the official port numbers used in computer networks. These numbers are usually assigned to a particular protocol or application. For example, 80 for HTTP and 443 for HTTPS. You can access standard applications using these numbers.
  • Choosing Your Own Custom Port Number: If you are developing your own application or want to use a specific port number, you can choose an available port number. However, you must ensure that this port number is not reserved by IANA or used by any other application.

3. Sending Data : You can use the sendto() function to send the data to the destination computer using the socket. You may need to encode the data you want to send as a byte array.

4. Closing the Socket : After sending the data, you must close the socket.

udp_socket.close()

As written above, UDP does not provide reliability. This does not guarantee whether the sent data reaches the other party precisely or that its order is maintained. TTL, on the other hand, is used to control the travel of data packets across the network and helps avoid loops. Therefore, a packet whose TTL value reaches zero is lost in the network and is not secure, which is a method that can be used for data packets sent with a communication protocol that does not provide reliability, such as UDP.

1.2 TCP

TCP (Transmission Control Protocol) is a communication protocol that provides reliable and sequential data transmission over the internet. This protocol ensures that data is not lost and its order is preserved. TCP takes a connection-oriented approach, meaning it establishes a connection before communication begins and closes the connection when communication ends. In this way, it is used in many applications that require reliable data transmission, such as websites, email servers, file transfers and database connections.

Data transfer with TCP is the same as UDP has, but with one difference. After specifying the target computer and port, we must connect the server. TCP is a protocol based on the client-server model, so you need to connect to the target server as a client. You can connect to the server with the connect() function:

tcp_socket.connect((target_ip, target_port))

The TCP data packet structure is quite complex and consists of two main parts: header and data. In this article, we will not talk more about the TCP data packet structure. But the picture was left for general understanding.

Fig.2 — TCP Frame Structure

Is it necessary to be connected to the internet to use TCP/UDP protocols?

So, is it necessary to be connected to the internet to send data using TCP and UDP protocols?
Absolutely not. TCP and UDP protocols do not require an internet connection to transmit data. Thanks to these protocols, computers can communicate with each other in the same local network or in different networks, even if they are not connected to the Internet. Network equipment such as switches or Moxa can be used to provide this communication.

Fig.3 — Moxa Devices

Which one is better?

TCP and UDP are two different protocols that meet different communication needs. Which protocol makes more sense depends on the requirements of the application to be used and the usage scenario.

--

--