1. Introduction

Streams and datagrams are two essential data transfer techniques in network programming.

Streams allow for the sequential transmission and reception of data, processing one byte at a time. Datagrams are independent data packets transferred over a network without establishing a direct connection between the parties.

In this tutorial, we’ll discuss the technical details of these two communication channels in greater detail.

2. What’s a Stream?

A network stream is a flow of data transmitted over a network connection sequentially, one byte after another.

Data flows from the sender to the receiver in a continuous stream instead of discrete packets, making data transfer and processing more efficient.

Usually, the Transmission Control Protocol (TCP) facilitates this kind of communication by guaranteeing dependable, well-organized data transmission. By connecting the sender and the recipient, TCP ensures that data packets arrive accurately and in the correct sequence:

Figure shows how a sender transmits bytes into the TCP connection, while the receiver extracts bytes from the TCP connection.

TCP sends a packet to the client after getting enough bytes from the server’s application process in the send buffer. On the client side, TCP empties the packet’s contents into a receive buffer, which the client’s application process reads.

Operating systems provide libraries that handle the intricacies of network communication, so programmers are free to concentrate on reading and writing data instead of dealing with low-level network details.

2.1. What Are the Features of a Stream?

The following table shows the features of streams:

Feature

Description

Sequential Data Flow

Enables constant and seamless communication by sending and receiving data in an ordered, continuous sequence.

Connection-Oriented Protocols

Streams generally use these protocols, such as TCP, which create a dependable connection between the sender and the recipient before starting the transmission. This guarantees accurate and sequential data delivery.

Reliability

Data delivery is ensured. The packet retransmission continues until the recipient confirms the correct data receipt.

Ordered Delivery

Perfect for applications like file transfers and video streaming, where the order of data is crucial (because the data must be received in the exact order that it was sent).

Flow Control

Streaming protocols can prevent the sender from sending too much information to the recipient at once. This guarantees effective and fair communication.

Byte-Oriented

Streams usually handle data as a continuous series of bytes and function at the byte level to control data transmission.

Abstraction Layer

Operating systems manage the intricacies of network communication by offering stream abstractions via libraries. This frees programmers to deal with network details so they can focus on developing apps for using data.

Versatility

Can perform several tasks, including file transfers and live video streaming.

As we see, streams are essential in situations requiring dependable and organized data transfer.

2.2. Stream Examples

In a stream socket communication (e.g., a chat application), a network socket sends each message as a stream of bytes using TCP, and the chat application exchanges data to and from the socket’s output and input streams in the right order without any data loss.

Similarly, when we load a webpage (e.g. when we browse the web), the browser establishes a connection with the server using a TCP stream.

Another example is file transfers (e.g., downloading and uploading data): the server reads data from a file and sends it as a stream of bytes to the client.

3. What Are Datagrams?

Datagrams are data packets that travel freely across a network and don’t need a pre-existing connection. As such, they don’t guarantee the order or delivery time.

The payload, frequently used for connectionless communication protocols like UDP (User Datagram Protocol), includes the actual data, whereas the header carries routing information.

3.1. Datagram Structure

A datagram consists of a basic header and data payload:

UDP datagrams consist of a simple header and data payload

The UDP header is 8 bytes long, with each of its four fields having 2 bytes.

  • The source port is an optional element and can be set to zero if unused. It contains the sender’s port number.
  • The port number of the intended recipient is in the second field, the destination port.
  • Length, the third field, specifies the UDP datagram’s overall length, including the header and the data. It must be at least 8 bytes long.
  • The checksum is the last field in the header. We use it to check the integrity of the header and data.

Finally, the payload comes after the header and contains the sent data.  The payload size is the total length (the length field) minus the length of the UDP header (8 bytes).

3.2. What Are the Features of Datagrams?

The following table shows the features of datagrams:

Feature

Description

Connectionless

A datagram doesn’t require a connection before it can send data

Unreliable Delivery

There’s no assurance that datagrams will arrive at their destinations. Occasionally, they may not arrive in order or may be lost.

No Ordering

It’s up to the application to handle reordering if necessary, as datagrams can arrive in any order.

Minimal Overhead

Have minimal overhead, resulting in faster transmission, without the need to establish connections or control flow.

Efficiency

Suitable for applications that require fast, efficient communication and where occasional data loss is acceptable.

Scalability

They are highly scalable due to their connectionless nature, so they can handle many concurrent transmissions.

Versatility

Used in several real-time applications, such as VoIP, live video streaming, online gaming, and DNS queries.

Datagrams are autonomous, self-contained data packets sent without assurances regarding error checking, delivery, or order.

They are perfect for real-time applications where speed is more important than dependability because they provide low-latency communication.

3.3. Datagram Examples

In real-time video streaming (e.g., live broadcasts), datagram sockets exchange data as datagrams using UDP to ensure low-latency delivery, even if some packets are dropped in transit.

Similarly, online gaming (e.g., multiplayer games) uses UDP datagrams to send game state updates rapidly without waiting for acknowledgments, keeping the gameplay fast and responsive.

Voice over IP (VoIP) calls (e.g., internet phone calls) also use UDP datagrams to transmit voice data efficiently. Speed is prioritized over perfect reliability to maintain real-time communication.

4. Stream vs. Datagram

Here’s a comparison of streams and datagrams:

Feature

Stream (TCP)

Datagram (UDP)

Connection

Connection-oriented and requires a link before data transfer.

Connectionless: requires no connection as each packet travels separately.

Reliability

Reliable: uses retransmissions and acknowledgments to guarantee data delivery.

Unreliable: no assurance of data integrity, order, or delivery.

Ordering

Ordered: information is sent and received in the same order.

Random: packages might not arrive in the correct order.

Overhead

Higher overhead because of connection setup, flow control, and error checking.

Lower overhead since there is no connection setup or flow control

Flow Control

Manages data transfer speeds by putting flow control into practice

There is no flow control, which could cause packet loss when congestion occurs.

Congestion control

This is implemented to prevent network congestion.

There are no procedures in place to control congestion.

Data Transfer

Byte-oriented: information is sent in the form of an uninterrupted number of bytes

Uses message-oriented datagrams or discrete packets to transport data.

Common Use

Ideal for applications that require dependable, ordered data transmission, like file transfers, web browsing, and real-time streaming

Suitable for applications that require fast, efficient transmission where sporadic packet loss is acceptable, like online gaming, live broadcasts, and VoIP

One of the primary distinctions between datagrams and streams is the balance between speed and dependability, ingrained in the corresponding protocols.

As a stream protocol, TCP values dependable data transfer. By implementing error detection, retransmission, and flow control systems, TCP guarantees that every data packet is received precisely and in the right order.

UDP, on the other hand, is a datagram protocol that prioritizes low latency and speed. UDP enables faster data transmission by eliminating the overhead related to connection establishment and error recovery.

4.1. How Do We Select Between Streams and Datagrams?

The choice between streams and TCP and datagrams and UDP depends on the particular nature of our application.

Streams and TCP are particularly suitable for applications where dependability is essential, such as file transfers (FTP), email (SMTP), online surfing (HTTP/HTTPS), and remote sessions (SSH).

On the other hand, UDP is perfect for real-time applications like voice over IP (VoIP), online gaming, live streaming, and DNS queries, where speed is more important than dependability. For example, losing a frame or two in a two-hour game stream will probably not be a big deal for viewers, but a constant lag will.

So, TCP and streams are suitable for cases where data integrity is important. At the same time, datagrams and UDP are good for speed, where low latency is crucial and packet loss tolerance is acceptable.

Generally speaking, we can use datagrams for real-time, smaller, time-sensitive data, such as multiplayer online games, and streams for non-real-time, massive data transfers.

5. Conclusion

In this article, we explored streams and datagrams, two essentially distinct data transport techniques used in network programming.

Streams offer dependable and well-organized data transmission for applications where data integrity and sequence are crucial. They are connection-based and have flow control and congestion management features, guaranteeing that data packets reach the recipient precisely and in the right order.

However, UDP-enabled datagrams offer a connectionless communication technique that prioritizes speed and low latency over dependability. Because each datagram travels separately, they offer faster data transmission and less overhead, negating the requirement for a pre-established connection.


原始标题:What Are the Differences Between Streams and Datagrams in Network Programming?