networking · advanced · ~15 min
`socket`/`connect`/`read`/`write` against a local peer.
Implement int tcp_echo(const char *host, int port, const char *msg, char *out, size_t out_sz) that:
host:port,msg followed by \n,out_sz - 1 bytes into out and NUL-terminates,The grader runs a tiny echo server on 127.0.0.1:<random port> — your client only talks to loopback. Do not connect anywhere else.
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stddef.h>
int tcp_echo(const char *host, int port, const char *msg, char *out, size_t out_sz) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.