networking · intermediate · ~15 min · safe pentest lab

Send a message client → server (length-prefixed)

Length-prefixed framing and looped send/recv.

Challenge

Implement two helpers:

int send_framed(int fd, const char *msg);          /* returns 0 on success */
int recv_framed(int fd, char *buf, int cap);       /* returns msg length on success */

Frame format: 4-byte length (network byte order), followed by length bytes of payload. Both helpers must handle short writes/reads with a loop.

Why this matters

TCP has no message boundaries — frame your messages with a length prefix so the receiver knows where one ends.

Starter code

#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int send_framed(int fd, const char *msg) { (void)fd; (void)msg; return -1; }
int recv_framed(int fd, char *buf, int cap) { (void)fd; (void)buf; (void)cap; return -1; }

Background lessons

Up next

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