Difference Between TCP and UDP Protocol
Difference Between TCP and UDP Protocol
Difference Between TCP and UDP Protocol
9DifferencebetweenTCPandUDPProtocolJavaNetworkInterviewQuestion
1/6
9/14/2015
9DifferencebetweenTCPandUDPProtocolJavaNetworkInterviewQuestion
interview question better but also understand some important details about two of
the most important protocols of internet.
2) Reliability
TCP provides delivery guarantee, which means a message sent using TCP protocol is
guaranteed to be delivered to client. If message is lost in transits then its recovered
using resending, which is handled by TCP protocol itself. On the other hand, UDP is
unreliable, it doesn't provide any delivery guarantee. A datagram package may be lost
in transits. That's why UDP is not suitable for programs which requires guaranteed
delivery.
3) Ordering
Apart from delivery guarantee, TCP also guarantees order of message. Message will be
http://javarevisited.blogspot.in/2014/07/9differencebetweentcpandudpprotocol.html
2/6
9/14/2015
9DifferencebetweenTCPandUDPProtocolJavaNetworkInterviewQuestion
delivered to client in the same order, server has sent, though its possible they may
reach out of order to the other end of the network. TCP protocol will do all
sequencing and ordering for you. UDP doesn't provide any ordering or sequencing
guarantee. Datagram packets may arrive in any order. That's why TCP is suitable for
application which need delivery in sequenced manner, though there are UDP based
protocol as well which provides ordering and reliability by using sequence number and
redelivery e.g. TIBCO Rendezvous, which is actually a UDP based application.
4) Data Boundary
TCP does not preserve data boundary, UDP does. In Transmission control protocol,
data is sent as a byte stream, and no distinguishing indications are transmitted to
signal message (segment) boundaries. On UDP, Packets are sent individually and are
checked for integrity only if they arrived. Packets have definite boundaries which are
honoured upon receipt, meaning a read operation at the receiver socket will yield an
entire message as it was originally sent. Though TCP will also deliver complete
message after assembling all bytes. Messages are stored on TCP buffers before
sending to make optimum use of network bandwidth.
5) Speed
In one word, TCP is slow and UDP is fast. Since TCP does has to create connection,
ensure guaranteed and ordered delivery, it does lot more than UDP. This cost TCP in
terms of speed, that's why UDP is more suitable where speed is a concern, for
example online video streaming, telecast or online multi player games.
7) Header size
TCP has bigger header than UDP. Usual header size of a TCP packet is 20 bytes which
is more than double of 8 bytes, header size of UDP datagram packet. TCP header
http://javarevisited.blogspot.in/2014/07/9differencebetweentcpandudpprotocol.html
3/6
9/14/2015
9DifferencebetweenTCPandUDPProtocolJavaNetworkInterviewQuestion
contains Sequence Number, Ack number, Data offset, Reserved, Control bit, Window,
Urgent Pointer, Options, Padding, Check Sum, Source port, and Destination port.
While UDP header only contains Length, Source port, Destination port, and Check
Sum. Here is how TCP and UDP header looks like :
4/6
9/14/2015
9DifferencebetweenTCPandUDPProtocolJavaNetworkInterviewQuestion
the other hand, UDP does not have an option for flow control.
That's all about difference between TCP and UDP protocol. Always remember to
mention that TCP is connection oriented, reliable, slow, provides guaranteed delivery
and preservers order of messages, while UDP is connection less, unreliable, no
ordering guarantee, but fast protocol. TCP overhead is also much higher than UDP, as
it transmits more meta data per packet than UDP. It's worth mentioning that header
size of Transmission control protocol is 20 bytes, compared to 8 bytes header of User
Datagram protocol. Use TCP, if you can't afford to lose any message, while UDP is
better for high speed data transmission, where loss of single packet is acceptable e.g.
video streaming or online multi player games. While working in TCP/UDP based
application on Linux, it's also good to remember basic networking commands e.g.
telnetand netstat,they help tremendously to debug or troubleshoot any
connection issue.
http://javarevisited.blogspot.in/2014/07/9differencebetweentcpandudpprotocol.html
5/6