TCP and UDP
There are two major transport level protocols that we're going to use — TCP and UDP. Protocol is a sort of a set of agreements of how does data transporting should work.
Transmission Control Protocol — TCP
Simply put, TCP connection is almost like a file — you open it, you read some data from it, you write some other data into it, you close it when you're done. There are some restrictions though:
When working with a file you can ask for its size. In case of TCP connection you can't.
You can position read or write pointer when you deal with a file. You also can't do this with TCP connection.
In other words, a file gives you random access while TCP connection is a sort of bidirectional sequential stream.
User Datagram Protocol — UDP
Data transmission over UDP represents exchange of solid data pieces. Compared to TCP, there is no such thing as UDP connection. You can't receive a part of data piece that has been sent to your application. You will get a whole piece either nothing. The things you need to know about UDP so far:
There is no UDP connection since it's not a stream. Hence you don't need to connect or close a UDP socket. All you need is to send data or to receive it.
The buffer you're using to receive UDP packet must be large enough to contain the whole packet in one piece, or else you will receive nothing. Hence you need to know the upper bound of packets size that you're going to receive.
The order of incoming packets is generally not the same as the order of their sending. This means that you need to maintain the order by yourself.
It is actually not guaranteed that you'll receive all packets that were sent to your application. This means that UDP packet loss is an ordinary thing. Which in turn means that you need control UDP packets delivery by yourself as well.
As you see, UDP is a bit more tricky thing than TCP. Still it has its advantages which we will discuss later.
This is all you need to know about protocols so far. And we can move forward.