Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a TCP Socket and a Connected UDP Socket

Default UDP socket plus the Connect() call is a Connected UDP Socket. I know the differences related to TCP and UDP but here I want to know the Difference in just the sockets

like image 972
arjun gulyani Avatar asked Feb 25 '16 11:02

arjun gulyani


2 Answers

When using a TCP socket, the tcp stack takes care of the data being sent to the network and being delivered to the receiver, retransmitting it until acknowledged by the receiver. TCP also takes care of flow control, i.e. transmitting the data at a suitable rate for the network connection and receiver. Last, TCP ensures that the receiver gets the data exactly once and in the correct order.

With UDP, the programmer manages the transmission to network directly, and has to take care of lost and out-of order packets as well as flow control and fragmenting data to packets that can be transmitted over the network connection.

Last, as udp and tcp are different protocols, they require different settings in firewalls to allow passing through to the server if the server is behind a firewall. Also you can't send data from a connected (or non-connected) UDP socket to a TCP socket or vice versa.

Conneting the UDP socket just means that send() and recv() can be used to send data to and from the connected address, but it is still UDP data being sent and received so all of the above differences apply. Calling connect() on a UDP socket is something you would do if you do only point-to-point communications using the socket.

like image 171
Sami Sallinen Avatar answered Sep 21 '22 04:09

Sami Sallinen


As it is widely known, transport protocols as TCP and UDP multiplex a single IP address at the source of the transmission and demultiplex another IP address at the destination. That is why port numbers came along. The way in which both protocols multiplex/demultiplex an IP address is what makes their sockets different.

In order to transmit some data with TCP, you simply drop a datagram in the socket and it will flow through a previously created connection. In turn, UDP is connectionless. The existence or absence of a connection requires that the identifier format of each socket be different: whereas a TCP socket is identified by the quadruple {source IP address, source port number, destination IP address, destination port number}, an UDP socket is identified by the tuple {destination IP address, destination port number}.

By the above, when a TCP service receives some datagram, it needs to know who sent it (“who” is an IP address + a port number) in order to properly forward it to the application. So, when two TCP datagrams with different source IPs arrive to a host, they will necessarily be delivered to two different sockets (even if the destination is the same). If there were no distinct sockets, there would be no connection handling at all (i.e., TCP would be connectionless).

With an UDP socket, the rule is different: if two arriving UDP datagrams have the same destination IP address and port number, they will be forwarded to the same socket, regardless their source IP addresses and/or source port numbers (they may be equal or not). Why bother with different sockets if the transmission is connectionless?

Remember, independently of their respective socket identifier formats, both TCP and UDP datagrams identify the port number of the sender; i.e., even a connectionless protocol as UDP have to identify the sender of the transmission as Kurose and Ross explain in their pretty good book “Computer Networking - a Top Down Approach”:

You may be wondering now, what is the purpose of the source port number? ... in the A-to-B segment the source port number serves as part of a “return address” — when B wants to send a segment back to A, the destination port in the B-to-A segment will take its value from the source port value of the A-to-B segment (the complete return address is A’s IP address and the source port number).

A final remark on nomenclature: here I used the term “datagram” generically, denoting the Protocol Data Unit (PDU) of both TCP and UDP protocols. Kurose and Ross use “segment” with the same intent. However, rigourosly speaking, “segment” is the PDU of the TCP protocol whereas “datagram” is the PDU of the UDP.

like image 9
Humberto Fioravante Ferro Avatar answered Sep 24 '22 04:09

Humberto Fioravante Ferro