When a socket function, such as socket(), bind(), accept(), and so on, has an error on a Unix platform, the error number gets stored in the thread-global errno variable. On Windows, the error number can be retrieved by calling WSAGetLastError() instead. Again, we can abstract out this difference using a macro:
#if defined(_WIN32)
#define GETSOCKETERRNO() (WSAGetLastError())
#else
#define GETSOCKETERRNO() (errno)
#endif
In addition to obtaining an error code, it is often useful to retrieve a text description of the error condition. Please refer to Chapter 13, Socket Programming Tips and Pitfalls, for a technique for this.
With these helper macros out of the way, let's dive into our first real socket program.