2nd December 2014 by aegeuana_sjp_admin
TCP and UDP socket types
Before we dive into describing the most used socket types, let’s see what a socket really is.
A socket, also referred as network socket, is a way of establishing a communication between various computers/devices. It sits on top of the TCP/IP stack and makes use of the available ports (channels). The connecting device is commonly named as the client, the receiving device, the one which accepts the connection is named the server.
There are mainly three types of sockets but we are going to describe the most used once, the UDP(Datagram Sockets) and TCP (Stream Sockets). For the curious ones out there, the third type is the Raw Sockets.
Both sockets are used to send and receive data, the major difference between the UDP and TCP type of sockets consists of the way how they handle the connection and the flow of the communication.
The TCP type of socket is “connected” socket, once the client establishes a connection to the server the connection is kept alive and the communication can begin be it for sending or receiving data. Both parties, the client and the server, can choose to send data or terminate the connection at any given time. Normally the other end will be notifed about the termination of the conection. TCP type of sockets are very robust and guarantee the sending and receiving of the data. Because the connection is kept alive this allows the data to be confirmed upon receival and sending, hence the robustness and the gurantee that the data is sent/received. TCP sockets also implement a “flow control” , making sure the data is correctly sent and divided into packets, not sending more data than required. Sending data via TCP in addition is guaranteed to be in correct order upon receival, for instance sending “Hello” then “world” will result into “Hello World”.
The UDP type of socket is “connectionless”, the client is not really establishing a connection to the server but rather sending a “packet” to the host and port. The major difference with this type of socket is that the data is not guaranteed to be received by the server nor acknowledged. The advantage of this type of socket lies in eliminating the overhead of establishing and keeping the connection alive. Since there is no real connection, the overhead is minimal and sending data via UDP type of socket is faster than TCP, remember TCP will establish a connection and only then be able to begin the transfer of the data. UDP does not guarantee any flow control or order of the data, you may have a race condition where the second packet is arrived before the first one resulting in “World Hello”.