networking · intermediate · ~15 min · safe pentest lab
Accept → recv → send → close, all on the same per-client fd.
Write the classic first server: accept one client, read what they send, send it straight back, hang up.
Implement int echo_once(int listening_fd) that handles exactly one connection:
listening_fd.listening_fd: an already-bound, already-listening TCP socket (the grader opens it on 127.0.0.1).Return the number of bytes echoed back to the client, or -1 on any failure (failed accept, recv error, or send error).
client connects and sends "ping" (4 bytes)
-> echo_once returns 4, and the client reads back "ping"
The traditional first server. Accept one client, read whatever they send, write it back, close.
listening_fd: a bound, listening TCP socket on 127.0.0.1.
The number of bytes echoed back, or -1 on any failure.
Read at most 256 bytes; send them back unchanged; close the client fd before returning.
#include <sys/socket.h>
#include <unistd.h>
int echo_once(int listening_fd) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.