Windows Sockets
The Winsock API was developed as an extension of the Berkeley Sockets API into the Windows environment, and Winsock is supported by all Windows systems. Winsock's benefits include the following.Chapter 14), which, among other things, allows servers to scale when there is a large number of active clients.
Winsock Initialization
The Winsock API is supported by a DLL (WS2_32.DLL) that can be accessed by linking WS2_32.LIB with your program. The DLL needs to be initialized with a nonstandard, Winsock-specific function, WSAStartup, which must be the first Winsock function a program calls. WSACleanup should be called when the program no longer needs to use Winsock functionality. Note: The prefix WSA denotes "Windows Sockets asynchronous. . . ." The asynchronous capabilities will not be used here because threads can and will be used where asynchronous operation is required.WSAStartup and WSACleanup, while always required, may be the only nonstandard functions you will use. A common practice is to use #ifdef statements to test the _WIN32 macro (normally defined at compile time by Visual C++) so that the WSA functions are called only if you are building on Windows. This approach assumes, of course, that the rest of your code is platform-independent.
int WSAStartup (
WORD wVersionRequired,
LPWSADATA lpWSAData);
Parameters
wVersionRequired indicates the highest version of the Winsock DLL that you need and can use. Version 1.1 is generally adequate and ensures the widest possible Windows interoperability. Nonetheless, Version 2.0 is available on all Windows systems, including 9x, and is used in the examples. Version 1.1 is obsolete.Chapter 2.When a program has completed or no longer needs to use sockets, it should call WSACleanup so that WS2_32.DLL, the sockets DLL, can free resources allocated for this process.
Creating a Socket
Once the Winsock DLL has been initialized, you can use the standard (i.e., Berkeley Sockets) functions to create sockets and connect for client/server or peer-to-peer communication.A Winsock SOCKET data type is analogous to the Windows HANDLE and can even be used with ReadFile and other Windows functions requiring a HANDLE. The socket function is called in order to create (or open) a SOCKET and returns its value.
SOCKET socket (int af, int type, int protocol);
Parameters
The type SOCKET is actually defined as an int, so UNIX code will port without the necessity of using the Windows type definitions.af denotes the address family, or protocol; use PF_INET (or AF_INET, which has the same value but is more properly used with the bind call) to designate IP (the Internet protocol component of TCP/IP).type specifies connection-oriented (SOCK_STREAM) or datagram communications (SOCK_DGRAM), slightly analogous to named pipes and mailslots, respectively.protocol is unnecessary when af is AF_INET; use 0.socket returns INVALID_SOCKET on failure.You can use Winsock with protocols other than TCP/IP by specifying different protocol values; we will use only TCP/IP.socket, like all the other standard functions, does not use uppercase letters in the function name. This is a departure from the Windows convention and is mandated by the need to conform to industry standards.