一、WinSock
WSAStartup (MAKEWORD(2,0), &WSAData);
//之后执行失败的都要WSACleanup();
sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
WSAAsyncSelect (sock, hwnd, WM_SOCKET_NOTIFY, FD_CONNECT | FD_READ);
//设置connect为非阻塞。
//在FD_CONNECT | FD_READ消息发生时,触发WM_SOCKET_NOTIFY消息
connect(sock, (SOCKADDR *) &sa, sizeof (sa)) ;
WM_SOCKET_NOTIFY消息处理:
wEvent = WSAGETSELECTEVENT (lParam) ; // ie, LOWORD
wError = WSAGETSELECTERROR (lParam) ; // ie, HIWORD
switch(wEvent)
{
case FD_CONNECT:recv (sock, (char *) &ulTime, 4, MSG_PEEK) ;
case FD_READ:recv (sock, (char *) &ulTime, 4, 0) ;
}
处理完毕后,清理:
closesocket (sock) ;
sock = 0 ;
WSACleanup () ;