networking · intermediate · ~15 min · safe pentest lab
Length-prefixed framing and looped send/recv.
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.
TCP has no message boundaries — frame your messages with a length prefix so the receiver knows where one ends.
#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; }
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.