[html]
DWORD clientThreadID; SOCKET clientSock; char server_address[50] = {0}; char recv_message_client[256] = {0}; struct sockaddr_in server_ip; BOOL clientThreadRun = false; //client BOOL InItClientSock() { //Define Variable WORD wVersionrequested; WSADATA wsaData; wVersionrequested = MAKEWORD(2,0); //Start Sock int err = WSAStartup(wVersionrequested,&wsaData); if (err == -1) { MessageBox(0,"WSAStartup err", "error",MB_OK); return FALSE; } return TRUE; } BOOL ConnectSock() { SOCKET clientSockConnect; u_short port; //Ini Sock clientSock = socket(AF_INET,SOCK_STREAM,0); if (ServerSock < 0) { MessageBox(0,"scoker err", "err",MB_OK); return FALSE; } char *strPort = strstr(server_address, ":"); if( (strPort!=NULL) && (strlen(strPort)>2) ) { port = atoi(strPort+1); if(port==0) { MessageBox(0, "IP端口不对, 参考例子:192.168.12.134:8981", "提示", MB_OK); return FALSE; } memset(strPort, 0, sizeof(strPort)); } else { MessageBox(0, "IP地址不对, 参考例子:192.168.12.134:8981", "提示", MB_OK); return FALSE; } //Connect server_ip.sin_family = AF_INET; server_ip.sin_port = ((port&0xff00)>>8)|((port&0x00ff)<<8); server_ip.sin_addr.S_un.S_addr = inet_addr(server_address); clientSockConnect = connect(clientSock,(struct sockaddr*)&server_ip,sizeof(server_ip)); if (clientSockConnect!=0) { CString temp; temp.Format("系统拒绝连接,可能是输入IP地址或端口不对:\n%s:%d", server_address, server_ip.sin_port); SetDlgItemText( FindWindow(NULL, WINDOWNHANDLERNAME), IDC_STATIC_IP, temp); MessageBox(0, "系统拒绝连接,可能是输入IP地址或端口不对 ", "提示", MB_OK); return FALSE; } return TRUE; } DWORD WINAPI clientThreadFunc(LPVOID threadNum) { int length; CString temp; if(!InItClientSock()) return 0; if(!ConnectSock()) return 0; //等待连接的时候,连接可能被取消。 if(clientThreadRun) SetDlgItemText(FindWindow(NULL, WINDOWNHANDLERNAME), IDC_STATIC_IP, "已经连接上!"); else { SetDlgItemText(FindWindow(NULL, WINDOWNHANDLERNAME), IDC_STATIC_IP, "请启动服务!"); return 0; } //send(NewSock,(char*)&FALG,sizeof(FALG)+1,MSG_OOB); while(clientThreadRun) { if( (length = recv(clientSock,(char*)recv_message_client,sizeof(recv_message_client),0))>0) { //MessageBox(0, recv_message, "接收到的信息", MB_OK); temp.Format("接收到的信息:\n%s",recv_message_client); SetDlgItemText(FindWindow(NULL, WINDOWNHANDLERNAME), IDC_STATIC_IP, temp); analyseCommand(recv_message_client); memset(recv_message_client, 0, sizeof(recv_message_client)); } temp.Format("接收到的信息:\n%s",recv_message_client); } return 0; }Android在wifi下手机与电脑的socket通信(2)
内容版权声明:除非注明,否则皆为本站原创文章。