Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 79 ยท Transport Layer

Socket Programming Basics

Learn the socket API: bind, listen, accept and connect, why TCP is a byte stream without message boundaries, how to frame messages, and blocking vs non-blocking I/O.

Everything in the last few lessons happens inside the operating system's network stack. Your program talks to it through one small interface: the socket API. It was introduced in 4.2BSD Unix in 1983 and is still, with minor variations, how almost every language opens a network connection. Whether you call it socket.connect in Python, net.connect in Node.js, TcpClient in C# or Socket in Java, you are using the same ideas from this lesson.

A socket is an endpoint of communication. For TCP, a connected socket is identified by five values: the protocol, local IP address, local port, remote IP address and remote port. (This is the "ports and sockets" idea from the transport layer basics.) In this lesson you will see the order of calls a client and server make, why TCP delivers a stream of bytes rather than messages, how programs add their own message boundaries, and how servers handle many connections at once. The examples run live as a Node.js server and client talking over the loopback interface.

The sequence of calls

A TCP server and client follow a fixed script. The names come from the C API; other languages wrap them, but keep the same order.

sequenceDiagram
    participant S as Server
    participant C as Client
    Note over S: socket() then bind(port) then listen()
    Note over C: socket()
    C->>S: connect() sends SYN
    S-->>C: SYN-ACK
    C->>S: ACK
    Note over S: accept() returns a new connected socket
    C->>S: send("hello")
    S->>C: send("HELLO")
    C->>S: close() sends FIN
    S->>C: close() sends FIN

The kernel completes the three-way handshake; accept() just hands the finished connection to the program.

On the server:

  1. socket() creates a socket and returns a handle (a file descriptor on Unix).
  2. bind() attaches it to a local address and port. Binding to 127.0.0.1 accepts only local connections, and 0.0.0.0 accepts connections on every interface. Port 0 asks the system to pick a free port.
  3. listen() turns it into a listening socket and sets the backlog, the length of the queue of connections that have completed the handshake but have not yet been accepted.
  4. accept() waits for the next connection from that queue and returns a new socket for that specific client. The listening socket stays open and keeps accepting.
  5. recv() and send() (or read/write) exchange bytes on the connected socket.
  6. close() ends the conversation.

On the client:

  1. socket() creates the socket.
  2. connect() starts the handshake to the server's address and port. The operating system picks a free ephemeral port as the local port; a client rarely calls bind().
  3. send()/recv() and close() as above.

Every connection ends up with its own server-side socket, but all of them share the same local port, since the remote address and port differ. That is why one server on port 443 can handle thousands of clients: the sockets are told apart by the full five values.

Node.js A first TCP server and client over loopback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that the program never asks for a message. The server's data callback fires whenever bytes arrive, whatever their size. That brings us to the most important fact about TCP programming.

TCP is a byte stream, not a message stream

A send("hello") followed by a send("world") does not guarantee that the other side will see two recv results, "hello" and "world". It may see "helloworld" in one read, or "hel" and "loworld". TCP promises only that the bytes arrive complete and in order. It tells you nothing about how they were grouped. Several things cut or join the data:

  • the sender's TCP may merge small writes into one segment (Nagle's algorithm), or split a big write into many segments (MSS);
  • the receiver's buffer and the application's read size decide how much one recv returns;
  • the network can deliver data in bursts.

The rules that follow are important enough to memorize:

  • A read may return fewer bytes than you asked for (a "short read"), including just one byte.
  • A write may accept fewer bytes than you offered (a "short write"), though many libraries loop for you.
  • A read that returns zero bytes (or an end event) means the peer closed its sending side. It is not an error.

Programs that treat one recv as one message work in testing on loopback and fail unpredictably on a real network. The cure is framing: the protocol itself must say where a message ends.

Common framing strategies

Strategy How it works Example
Delimiter A special byte or sequence ends each message. Lines ending in \n or \r\n: SMTP, Redis protocol parts, many chat protocols.
Length prefix A fixed-size number tells how many bytes follow. A 4-byte big-endian length then the payload: many binary protocols.
Fixed size Every message has the same length. Simple binary records.
Close The end of the connection ends the message. Old HTTP/1.0 responses with no length.
Header with length, then body Text headers that include Content-Length. HTTP.

Delimiters are easy to read and debug, but the payload must never contain the delimiter (or must escape it, as in byte stuffing at the link layer). A length prefix handles any bytes, but both sides must agree on the size and byte order, and the receiver must refuse absurd lengths so a bad peer cannot make it allocate gigabytes.

The decoder at the receiving end must keep a buffer: append new bytes, then extract as many complete messages as the buffer holds and keep the remainder for next time. The example below sends three messages, cuts them into pieces of three bytes with small pauses to force heavy fragmentation, and still receives each message intact.

Node.js Length-prefixed framing over a fragmented stream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The wire carries 4 + 5 + 4 + 24 + 4 + 5 = 46 bytes. However the stream is chopped up, the decoder gets the same three messages, because it relies on the lengths and not on the read boundaries.

The same lesson applies to text protocols. If you receive lines, you must buffer partial lines. A common bug is calling split('\n') on every chunk without keeping the incomplete last piece.

Handling many clients

A server rarely talks to only one client. How a program waits for data from many sockets at once is one of the central design questions of network programming.

  • Blocking I/O, one thread (or process) per connection. recv() simply waits (blocks) until data comes. This is simple to write: each connection has its own loop. But thousands of threads cost memory and scheduling time.
  • Non-blocking I/O with an event loop. The socket is switched to non-blocking mode, so calls return immediately with "would block" when nothing is ready. The program asks the kernel which sockets are ready using select, poll, epoll (Linux), kqueue (BSD/macOS) or IOCP (Windows), and serves only those. One thread can manage tens of thousands of connections. This is how Node.js, Nginx and most modern servers work.
  • Asynchronous I/O (async/await). A language-level wrapper over the event loop. The code reads like blocking code, but each await hands control back to the loop.

Node.js always uses the second model, so a single JavaScript thread serves many sockets. The next example opens three connections that ask the server to wait for 60, 40 and 20 milliseconds before replying. A blocking one-at-a-time server would answer them in the order they connected; the event-driven server answers the quickest first, without starting any threads.

Node.js One thread, three concurrent connections

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

When things go wrong

Network code has to expect failures, and many errors map directly to TCP behavior you already know.

Error Meaning Underlying cause
ECONNREFUSED Connection refused A RST came back: nothing listens on that port (see teardown and RST).
ETIMEDOUT Connection timed out SYNs went unanswered: dropped by a firewall or the host is down.
ECONNRESET Connection reset by peer The peer sent RST while you were reading or writing.
EPIPE Broken pipe You wrote to a connection the peer already closed.
EADDRINUSE Address already in use Another socket is bound to that port (or a TIME_WAIT entry exists; servers usually set SO_REUSEADDR).

Always set timeouts: a connection attempt, a read and an idle connection can each hang for minutes or forever if you let the defaults decide. And always handle the error events; in Node, an unhandled error event on a socket crashes the process.

Node.js Provoking ECONNREFUSED on purpose

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

In Python the same ideas appear with the standard socket module, which exposes the C-level calls almost directly. The shape of the code (bind, listen, accept, loops that handle short reads) is identical.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A server calls bind(), listen() and accept(). What does accept() return?

    accept() takes a finished connection off the queue and returns a new socket dedicated to that client. The listening socket stays open for further clients.
  2. 2. A client sends 'hello' and then 'world' in two send() calls. What can the server's recv() calls see?

    TCP is a byte stream. It keeps the bytes in order but does not preserve write boundaries, so the program must frame messages itself.
  3. 3. Which is a valid way to tell where one message ends in a TCP stream?

    Only information inside the stream, such as a length prefix or delimiter, is reliable. Segment boundaries and flags are not visible or dependable at the application level.
  4. 4. What does a read returning zero bytes (an 'end' event) mean for a TCP socket?

    End-of-stream means the peer finished sending. 'No data yet' is reported differently (for example EWOULDBLOCK), and a reset shows up as an error.
  5. 5. Why can a single-threaded event-loop server handle thousands of connections?

    With readiness notification (epoll, kqueue, IOCP) one thread only touches sockets that have work to do, instead of blocking on each one.

Summary

  • The socket API is the program's doorway to TCP and UDP. Servers call bind, listen and accept; clients call connect. accept() returns a new socket per client, and sockets are identified by protocol, local and remote address and port.
  • TCP delivers a byte stream: reads and writes may be split or merged arbitrarily, and a read of zero bytes means the peer closed.
  • Applications must frame their messages with delimiters, length prefixes or headers that carry a length, and keep a buffer for partial data.
  • Servers choose between thread-per-connection with blocking I/O and a single-threaded event loop with non-blocking I/O (epoll, kqueue, IOCP), which scales to many connections.
  • Common errors map to TCP events: ECONNREFUSED is a RST from a closed port, ETIMEDOUT is silence, ECONNRESET is an abrupt RST. Always use timeouts and handle error events.

Next, write the same kind of programs in Python and see how sockets work in Python.