TCP client code

We begin our TCP client by including the header file, chap03.h, which was printed at the beginning of this chapter. This header file includes the various other headers and macros we need for cross-platform networking:

/*tcp_client.c*/#include "chap03.h"

On Windows, we also need the conio.h header. This is required for the _kbhit() function, which helps us by indicating whether terminal input is waiting. We conditionally include this header, like so:

/*tcp_client.c*/#if defined(_WIN32)#include <conio.h>#endif

We can then begin the main() function and initialize Winsock:

/*tcp_client.c*/int main(int argc, char *argv[]) {#if defined(_WIN32)    WSADATA d;    if (WSAStartup(MAKEWORD(2, 2), &d)) { fprintf(stderr, "Failed to initialize.\n"); ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.