
368 12. Network Programming
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{
WSACleanup();
status = WSAGetLastError(); // Get detailed error
return ( (status << 16) + NET_INVALID_SOCKET);
}
type = UNCONNECTED_TCP;
break;
default: // Invalid type
return (NET_INIT_FAILED);
}
Listing 12.1(b). Creating the network socket.
If an error is detected as part of the socket creation, WSACleanup is called to end
Windows socket operations and free resources. e function WSAGetLastError is called
to retrieve the error code, which is combined with our own error code and returned. e
code return ( (status << 16) + NET_INVALID_SOCKET); ...