1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
if(idTask == (HWND)wParam)
{
hostent *host = (struct hostent *) buf;
sockaddr *addr = NULL;
sockaddr_in addr_ip4;
size_t addrLen = 0;
if(WSAGETASYNCERROR(lParam) != 0)
{
MessageBox(hWnd, "WSAGETASYNCERROR Error!", NULL, MB_ICONERROR | MB_OK);
return 0;
}
/*addr_ip4.sin_family = host->h_addrtype;
addr_ip4.sin_port = htons(port);
memcpy(&(addr_ip4.sin_addr), host->h_addr_list[0], host->h_length);
addr = (struct sockaddr *)&addr_ip4;
addrLen = sizeof(addr_ip4);*/
addr_ip4.sin_family = AF_INET;
addr_ip4.sin_port = htons(port);
addr_ip4.sin_addr.S_un.S_addr = inet_addr(ip_addr);
if ((sock = socket(host->h_addrtype, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET){
MessageBox(hWnd, "Can't create socket!", NULL, MB_ICONERROR | MB_OK);
return 0;
}
if (connect(sock, addr, addrLen) == SOCKET_ERROR){
MessageBox(hWnd, "Can't connect to the server!", NULL, MB_ICONERROR | MB_OK);
return 0;
}
if (WSAAsyncSelect(sock, MainhWnd, WM_SOCKET, FD_READ) == SOCKET_ERROR){
MessageBox(hWnd, "Can't read from the socket!", NULL, MB_ICONERROR | MB_OK);
return 0;
}
}
break;
}
|