UDP "connection" basics

Top  Previous  Next

What is UDP

In many aspects, the UDP protocol is the opposite of the TCP protocol. Whereas the TCP provides a realiable, stream-oriented transport, the UDP offers a way to send data as separate packets or "UDP datagrams". This is similar to "paging" (as in, sending a message with a "pager" — remember those?). What is sent is a packet containing some data. There is no guarantee that the other side will receive the packet and the sender won't know whether the packet was received or not.

Each UDP datagram lives its own life and you are responsible for dividing your data into chunks of reasonable size and sending it in separate datagrams. There is no connection establishment or termination on UDP- the datagram can be sent instantly, without any preparation.

Unlike TCP, the UDP is not point-to-point. For example, several hosts can send the datagrams to the same socket of your device- and all of them can be accepted- a situation that is impossible with TCP. There is also an option to broadcast the datagram to all hosts connected to the same network segment- something that is also impossible with TCP.

And now to UDP "connections"...

This said, it should come as a bit of a surprise that we will now turn 180 degrees and start talking about UDP "connections". Didn't we just say that there is no such thing? Well, yes and no. On the physical network there is, indeed, no such thing. However, on our socket object level we have deliberately made UDP communications look more like TCP connections. And, since the UDP "connection" is nothing more than our sock object's abstraction, we use the word "connection" in quotation marks.

We consider an active open to have been performed when our host has sent the first UDP datagram to the destination. A passive open is when we have received the first incoming UDP datagram from another host (provided that this datagram was accepted- more on that in Handling Incoming Connections).

There is no graceful disconnect for UDP connections. The connection can only be discarded (our host "forgets" about it). The UDP connection can also timeout.