networking · advanced · ~15 min
Connectionless sockets, `sendto`/`recvfrom`.
Implement int udp_roundtrip(const char *host, int port, const char *msg, char *out, size_t out_sz) that sends one UDP datagram containing msg to host:port, then waits for one datagram in reply and copies it (NUL-terminated) into out. Return 0 on success.
The grader runs a tiny local UDP echo on 127.0.0.1.
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stddef.h>
int udp_roundtrip(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.