networking · advanced · ~15 min

One-shot TCP server

`bind`/`listen`/`accept`; the lifecycle of a listening socket.

Challenge

Implement int single_echo_server(int port) that:

  1. binds to 127.0.0.1:port,
  2. listens with backlog 1,
  3. accepts one client,
  4. reads up to 256 bytes, writes them back to the client,
  5. closes everything and returns 0.

The grader connects a client thread to port and asserts the round-trip.

Starter code

#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

int single_echo_server(int port) {
    /* TODO */
    return -1;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.