A simple UDP client

Although we've already shown a fairly full-featured UDP client, udp_client.c, it is worthwhile building a very simple UDP client. This client shows only the minimal required steps to get a working UDP client, and it uses sendto() instead of send().

Let's begin the same way we begin each program, by including the necessary headers, starting main(), and initializing Winsock, as follows:

/*udp_sendto.c*/#include "chap04.h"int main() {#if defined(_WIN32)    WSADATA d;    if (WSAStartup(MAKEWORD(2, 2), &d)) {        fprintf(stderr, "Failed to initialize.\n");        return 1;    }#endif

We then configure the remote address using getaddrinfo(). In this minimal example, we use 127.0.0.1 as the remote address and 8080 as the remote port. This means ...

Get Hands-On Network Programming with C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.