37
votes

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?

6

6 Answers

45
votes

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.

8
votes

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

5
votes

TCP is a connection-oriented protocol meaning it first sets up a connection to the receiver then sends the data in segments (PDU for transport layer) which is carried by IP packets. This way it's called stream because it keeps the stream of data between to ends during transfer.

UDP is a connection-less transport protocol (just like IP) with data unit called datagram. So unlike tcp, UDP transfers data without setting up a connection just sending down datagram messages to IP layer in order to be transferred.

3
votes

Lot of confusion here. Let me clarify.

TCP/IP is a stream-oriented, Packet and Connection oriented protocol. UDP is just a packet-oriented protocol. Doesn't establish connection first.

Let us say that you are you using a Java program to connect to a network in your application by calling java.net.Socket class on the client side and java.net.ServerSocket on the server side. Once the connection is established the data transmission starts. The question comes, is the data sent in stream (Codata or infinite stream) or packet if I chose TCP? The answer is data received by the TCP method is stream but TCP converts stream into packet before sending the lower lavel stack. Basically, the application layer above sends the data in stream to the TCP layer and TCP breaks it down into packets to the network layer, and performs packet-to-streaming while receiving fro the server (receiving) side because your application Java can understand only Stream. File transmission is preferred via TCP over UDP because you can't afford losing of packets.

UDP, on the other hand, is a packet-oriented protocol where the application such as Java class java.net.DatagramPacket; java.net.DatagramPacket; import java.net.DatagramsSocket creates a packet first before talking to UDP, and the packet is sent out with additional information by UDP/IP protocols to the server side. Note, some applications might present the data as a stream when the underlying protocol is UDP. However, this is the layering of an additional protocol on top of UDP, and it is not something inherent in the UDP protocol itself. Live straming of TV is generally UDP because you are not worried about loss of packets.

2
votes

The specialty about TCP is that this behaviour is transparent to the user resp. the app.

The only thing the app has to do is call send() and recv() on order to send and get data.

The layers below ensure that the data is received in exactly the order it was sent, and that missing data is retransmitted if it "stays missing".

UDP, OTOH, keeps the data of one send() call together, even if it is split into several IP packets. In this way, these data can be seen as one datagram.

1
votes

TCP and UDP both are transport layer protocols, both provides a process to process delivery (client to server), but they are very different from each other in the way they provide their services. the main difference between UDP and TCP is; UDP provides a connection-less service whereas TCP provides connection oriented services.

that's why TCP is reliable, now why we called TCP a stream oriented protocol?

As we all know TCP protocol keeps track of the segments being transmitted or received that's why it is reliable but still if you see TCP segment header, there is no field for a segment number value in the segment header. Instead, there are two fields called the sequence number and the acknowledgment number. These two fields refer to a byte number and not a segment number.

Byte Number: TCP numbers all data bytes (octets) that are transmitted in a connection. Numbering is independent in each direction. When TCP receives bytes of data from a process, TCP stores them in the sending buffer and numbers them. The numbering does not necessarily start from 0. Instead, TCP chooses an arbitrary number between 0 and ((2)**32)− 1 for the number of the first byte. For example, if the number happens to be 1,057 and the total data to be sent is 6,000 bytes, the bytes are numbered from 1,057 to 7,056.

Sequence Number: After the bytes have been numbered, TCP assigns a sequence number to each segment that is being sent. The sequence number for each segment is the number of the first byte of data carried in that segment.

Suppose a TCP connection is transferring a file of 5,000 bytes. The first byte is numbered 10,001. What are the sequence numbers for each segment if data are sent in five segments, each carrying 1,000 bytes?

TCP_segments

Segment 1 → Sequence Number: 10,001 Range: 10,001 to 11,000 Segment 2 → Sequence Number: 11,001 Range: 11,001 to 12,000 Segment 3 → Sequence Number: 12,001 Range: 12,001 to 13,000 Segment 4 → Sequence Number: 13,001 Range: 13,001 to 14,000 Segment 5 → Sequence Number: 14,001 Range: 14,001 to 15,000

this is the reason we called TCP a stream controlled protocol because it keeps track of every bytes send or received and acknowledges every segments.

whereas why we called UDP a message orientated protocol?

UDP provides a connection-less service, it means each user data-grams (UDP packet) are independent and keeps no relationship from each other even though if their source is same and going to same destination. One of the ramifications of being connection-less is that the process that uses UDP cannot send a stream of data to UDP and expect UDP to chop them into different related user data-grams (like TCP does). Instead each request must be small enough to fit into one user data-gram. so the each data-gram has boundary and message are self contained that can have meaning. that's why UDP is also called as message-oriented protocol.

for more detail please read TCP/IP protocol suite chapter #14 and #15 from Behrouz A. Forouzan

Hope this helps!