networking · advanced · ~15 min

UDP send and receive

Connectionless sockets, `sendto`/`recvfrom`.

Challenge

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.

Starter code

#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.