Communication over TCP and network traffic through connection setup, data delivery, and closure.

TCP Communication: From Connection to Close

TCP lets two applications exchange an ordered stream of bytes over a network. Communication using TCP starts with a connection, continues as each endpoint tracks sent and received data, and ends when the connection closes or fails.

The packets carrying TCP data may be lost, delayed, or arrive out of order. TCP detects loss and retransmits data, but a reliable transport does not mean the remote application has processed a request. That distinction shapes how you handle failures.

TCP Traffic: Establishing a Connection

A server listens on a local IP address and port. A client connects from its own address and port to the server’s endpoint. The combination of source and destination addresses and ports identifies a connection, allowing the server to handle multiple clients on the same listening port.

A typical connection begins with a three-way handshake:

  1. The client sends a SYN segment with an initial sequence number.
  2. The server replies with SYN and ACK, acknowledging the client’s SYN and offering its own initial sequence number.
  3. The client sends an ACK acknowledging the server’s SYN.

Each endpoint maintains state as this exchange progresses. Once established, both sides can send data independently. A handshake can fail or time out; an application must handle that outcome instead of assuming a listening port guarantees a usable connection. Explaining how TCP works begins with that shared connection state.

Data, Acknowledgments, and Retransmission

During tcp communication, sequence numbers identify positions in each direction’s byte stream. This exchange over TCP uses the numbers to put bytes in order and recognize duplicates. An acknowledgment indicates the next byte expected in that direction, so it can confirm receipt of a run of earlier bytes.

If an acknowledgment does not arrive as expected, the sender may retransmit data. A missing acknowledgment does not prove the original data was lost: the acknowledgment itself might have been lost. TCP uses sequence information to avoid delivering retransmitted bytes twice to the receiving application.

TCP also regulates transmission. The receiver advertises a window indicating how much data it is prepared to accept, while congestion control responds to conditions on the network path. These mechanisms help prevent overload, but they do not promise a fixed throughput or a fixed delivery time.

Here is TCP explained as an interface: the receiver gets an ordered byte stream, not the sender’s original message boundaries. If a sender writes a request in one call, the receiver might read it in several calls, or read bytes from multiple writes together. Define framing in the application protocol, for example with a length prefix or a delimiter, and handle partial reads and writes.

Closing and Failure

In a normal close, one endpoint sends FIN to indicate that it has finished sending. The peer acknowledges it and can still send its own remaining data before closing its sending direction. The other direction closes with its own FIN and acknowledgment. The exchange can involve separate or combined segments, and connection state can persist briefly after the last acknowledgment.

An abrupt reset or a network failure is different. A peer may disappear without a clean close, and a stalled connection may not reveal the problem immediately. Set application-appropriate timeouts and handle end-of-stream and socket errors. Retrying may be useful, but it can repeat an operation if the first request reached the server before the response was lost.

When inspecting network traffic over TCP, distinguish an acknowledgment from an application response. The acknowledgment means the receiving TCP endpoint accepted the bytes; it does not establish that the application parsed, committed, or acted on them. An application-level response or durable record is needed when that distinction matters.

What the Application Still Owns

TCP traffic supplies a reliable, ordered byte stream while the connection works; it does not supply message boundaries, encryption, business-level confirmation, or an indefinite guarantee that a peer remains available. Use TLS when confidentiality and peer authentication are required, and design framing, timeouts, and retry rules around the application’s operations.

Use TCP for transport and let the application decide how to detect incomplete work and recover safely. For the protocol details, see RFC 9293.