UDP protocol and header explained
Prerequisites- This article assumes a basic understanding of networking in general.
First of all, we have to know what UDP(User Datagram Protocol) is actually. UDP(User Datagram Protocol) is a communications protocol that facilitates the exchange of messages between computing devices in a network. That makes sense. But what makes it different from that TCP? Well, here you go … The only thing that makes it different from TCP(Transmission Control Protocol) — UDP is a connectionless protocol where us, TCP is a connection-oriented protocol. Although there are some differences that roots from this cause, we will just take only this difference for now. So, when two or more nodes are communicating over UDP protocol, they won’t initialize a connection. Rather, they will just exchange messages by targeting a specific address.
You might be asking yourself if the only difference is this, what is the problem then? Well, let me explain. So far, we understood that TCP is a connection-oriented protocol. So, for two nodes to communicate over TCP protocol, they have to initialize a connection. They made a connection means, the data are going just through a single pipe. So, there is no data loss while communicating. Ok, let’s come back to UDP now. Well, we know that UDP is a connectionless protocol. So, for two nodes to communicate, they will just exchange data directly instead of making connections. So, connectionless means, the data is not going through a single pipe. Rather it is just floating randomly over the network. This connectionless behavior may cause data loss while data is getting transferred because of the fact that the data are not going through a single pipe. And also the data might get corrupted because of the same reason.
Well, we are in big trouble now! The UDP protocol may corrupt our data or result in a loss of our data. So, what we should do now? Well, we have a very good solution here. I am gone introduce you to the concept of “UDP headers”.
While packaging data to transfer it over a UDP protocol and the receiver is receiving the data, we don’t know if the receiver received corrupted data or not. So, what is the solution? Well, one possible and the best solution is to use a checksum. Wait, what is a checksum?
A checksum is a value used to verify the integrity of a file or a data transfer. In other words, it is a sum that checks the validity of data. Checksums are typically used to compare two sets of data to make sure they are the same.
So, we calculate the checksum of the data. Then attach it to the actual data. And the receiver will receive the data. After the receiver got the data, we don’t know if it is corrupted or not. So, we have to calculate the checksum. So, we first extract the actual data and calculate the checksum for it. Then, we validate the checksum we got and the checksum we received in the packet. If the checksums are equal, the data is not corrupted otherwise the data is corrupted. But wait, how are we gone attach the checksum to the actual data? Are we gone send it by combining it with the data or what? Well, here comes the concept of UDP headers. UDP headers contain a set of parameters also called fields defined by the technical specifications of the protocol.
UDP header has four fields, each of which is 2 bytes in size. The parameters are the source port number, the destination port number, the packet(data) length, and the checksum. See the below image to understand its structure.
Wait, we mentioned that UDP headers contain a checksum. Great! Now you understood that we are not gone combine the checksum data with the actual data directly. Rather, we are gone combine it with the UDP header data. So the pattern is, we first make a UDP header data with the specified fields. if we don’t care about the other fields, we can fill the other fields(source port, destination port & length) with arbitrary data and fill the checksum field with the correct data. After that, we will attach this UDP header with the actual data and finally, send it to the receiver.
I know it seems weird to understand this theory without an experiment. But I have great news for you! I will be writing an article that we are going to experiment with the theory on it. Stay tuned!
Support me by following me on Medium. And also follow me on Twitter and Github. If you have any questions or feedback, shoot it in the comment section.