Socket Data Type

In Winsock applications, a socket descriptor is not a file descriptor and must be used with the Winsock functions.

In UNIX, a socket descriptor is represented by a standard file descriptor. As a result, a socket descriptor on UNIX may be passed to any of the standard file I/O functions (read and write, for example).

Furthermore, all handles in UNIX, including socket handles, are small, non-negative integers, and some applications make assumptions that this will be true.

Windows Sockets handles have no restrictions, other than that the value INVALID_SOCKET is not a valid socket. Socket handles may take any value in the range 0 to INVALID_SOCKET–1.

Because the SOCKET type is unsigned, compiling existing source code from, for example, a UNIX environment may lead to compiler warnings about signed/unsigned data type mismatches.

This means, for example, that checking for errors when the socket and accept functions return should not be done by comparing the return value with –1, or seeing if the value is negative (both common and legal approaches in UNIX). Instead, an application should use the manifest constant INVALID_SOCKET as defined in the Winsock2.h header file. For example:

Typical BSD UNIX Style

s = socket(...);
if (s == -1)    /* or s < 0 */
    {/*...*/}

Preferred Style

s = socket(...);
if (s == INVALID_SOCKET)
    {/*...*/}

Porting Socket Applications to Winsock

Winsock Programming Considerations