networking · intermediate · ~15 min · safe pentest lab

One-shot echo server

Accept → recv → send → close, all on the same per-client fd.

Challenge

Implement int echo_once(int listening_fd) that:

  1. Accepts one connection on listening_fd.
  2. Reads up to 256 bytes from the client.
  3. Writes those bytes back unchanged.
  4. Closes the client fd.
  5. Returns the number of bytes echoed, or -1 on failure.

Why this matters

The traditional first server. Accept one client, read whatever they send, write it back, close.

Starter code

#include <sys/socket.h>
#include <unistd.h>
int echo_once(int listening_fd) {
    /* TODO */
    return -1;
}

Background lessons

Up next

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