Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCP stream vs UDP message

TCP is stream oriented meaning data is transferred as a continues stream of bytes. But what confuses me is that TCP creates segments and passes this down to IP. IP creates packets encapsulates segments and transfers them. So where exactly the continuous stream here?

UDP on the other hand is message oriented. It receives messages from application layer, creates datagrams and pushes it down to IP. So far it is the same as TCP, instead a datagram created and pushed down. What makes this protocol a message oriented?

like image 898
Kuzey Avatar asked Jul 03 '13 11:07

Kuzey


People also ask

Is TCP message stream?

TCP is a stream oriented protocol and UDP is a message-oriented protocol. TCP receives the stream of bytes from application layer protocols and divide it in to segments and pass it to IP.

What is better for streaming UDP or TCP?

As for streaming it's better to use UDP, first because it lowers the load on servers, but mostly because you can send packets with multicast, it's simpler than sending it to each connected client.

Why is UDP message-oriented?

UDP on the other hand is message oriented. It receives messages from application layer, creates datagrams and pushes it down to IP. So far it is the same as TCP, instead a datagram created and pushed down.

Does TCP send packets as a stream?

Stream Versus Packet — TCP/IP is a stream-oriented protocol, while UDP is a packet-oriented protocol. This means that TCP/IP is considered to be a long stream of data that is transmitted from one end of the connection to the other end, and another long stream of data flowing in the opposite direction.


2 Answers

TCP is stream oriented because it is able to assemble data in contiguous format. E.g. you had data from number 1 to 4000 bytes. Now it will be divided into tcp segments where each segment would have a sequence number say first is 1-1200 byte, second is 1201 - 2400 and so on.

It might be delivered out of order while being sent through ip datagrams but is assembled into contiguous data latter, thereby appearing as a stream. The sequence number helps to reorder packets.

A little deeper explanation is:

A byte stream consist of one big chunk of data with no segments or other irregularities. With datagrams (smaller) data chunks are send and received at once as a whole. In practice it means that with datagrams each send/write call sends one packet, and each read/recv call receives one packet, while with stream protocol the data can be send and received in any way. E.g. A sender can call send() ten times, while the receiver receives all that data with one recv call. With datagrams ten send calls means ten packets and ten receive calls

Datagrams and streams

Byte streams

like image 21
fkl Avatar answered Sep 20 '22 16:09

fkl


The interface/API presented to you the user(programmer) of these protocols are:

UDP

Message oriented, you have an API (send/recv and similar) that provide you with the ability to send one datagram, and receive one datagram. 1 send() call results in 1 datagram sent, and 1 recv() call will recieve exactly 1 datagram.

TCP

Stream oriented, you have an API (send/recv and similar) that gives you the ability to send or receive a byte stream. There is no preservation of message boundaries, TCP can bundle up data from many send() calls into one segment, or it could break down data from one send() call into many segments - but that's transparent to applications sitting on top of TCP, and recv() just gives you back data, with no relation to how many send() calls produced the data you get back.

like image 70
nos Avatar answered Sep 23 '22 16:09

nos