结构体列表:
- WSADATA
- SOCKET
- SOCKADDR_IN
函数列表:
- WSAStartup
- socket
- htonl
- htons
- bind
- listen
- accept
- send
- recv
- closesocket
代码:
1 #pragma comment(lib, "wsock32.lib") 2 #include <WinSock2.h> 3 #include <stdlib.h> 4 #include <windows.h> 5 6 /*** 7 * @author:zanzan101 8 */ 9 10 static HANDLE event_server = CreateEvent(0, 0, 0, "server_event"); 11 static HANDLE event_client = CreateEvent(0, 0, 0, "client_event"); 12 static HANDLE event_end = CreateEvent(0, 0, 0, "end_event"); 13 14 class Thread 15 { 16 private: 17 HANDLE thread; 18 public: 19 Thread(int(*func)(int* para), void* para) // 故意简化了参数,但这并不是一个规范的做法 20 { 21 thread = NULL; 22 23 // 创建一个挂起的进程 24 thread = CreateThread(0, 0, reinterpret_cast<DWORD(CALLBACK*)(void*)>(func), para, CREATE_SUSPENDED, 0); 25 } 26 ~Thread() 27 { 28 CloseHandle(thread); 29 } 30 void resume() 31 { 32 ResumeThread(thread); 33 } 34 void suspend() 35 { 36 SuspendThread(thread); 37 } 38 void terminate() 39 { 40 TerminateThread(thread, 0); 41 } 42 43 }; 44 45 int server_func(int* para) 46 { 47 int wsa_res = 0; 48 // 打开 49 WSADATA wsa_data; 50 if(!(wsa_res = WSAStartup(0x0101, &wsa_data))) 51 printf("server_%d 打开成功! ", *para); 52 else 53 { 54 printf("server_%d ERROR:%d ", *para, wsa_res - WSABASEERR); 55 return -1; 56 } 57 58 // 创建套接字 59 SOCKET socket_server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 60 SOCKADDR_IN addr_server; 61 SOCKADDR_IN addr_client; 62 63 addr_server.sin_addr.S_un.S_addr=htonl(INADDR_ANY); 64 addr_server.sin_family=AF_INET; 65 addr_server.sin_port=htons(6666); 66 67 // 将套接字与端口号绑定 68 bind(socket_server, (SOCKADDR*)&addr_server, sizeof(SOCKADDR)); 69 70 // 监听端口 71 listen(socket_server, SOMAXCONN); 72 73 74 int len = sizeof(SOCKADDR_IN); 75 76 printf("server 准备完毕... "); 77 // 设置信号,通知client 78 SetEvent(event_server); 79 80 printf("server在等待client... "); 81 82 83 while(WaitForSingleObject(event_client, 0) != 0) 84 { 85 char recv_buf[100]; 86 char send_buf[100]; 87 88 ////////////////////////////////////////////////////////////////////////// 89 90 SOCKET socket_connect=accept(socket_server,(SOCKADDR*)&addr_client,&len); 91 int bytes = recv(socket_connect, recv_buf, 100, 0); 92 recv_buf[bytes] = 0; 93 int num = atoi(recv_buf); 94 if(bytes > 0 && -1 != num) 95 printf("server收到:%d ",num); 96 else if(-1 != num) 97 printf("server什么也没收到 "); 98 if(-1 != num) 99 { 100 sprintf(send_buf,"%d的平方是:%d ",num, num*num); 101 send(socket_connect,send_buf,strlen(send_buf)+1,0); 102 } 103 closesocket(socket_connect); 104 105 ////////////////////////////////////////////////////////////////////////// 106 } 107 108 if(!(wsa_res = WSACleanup())) 109 printf("server_%d 关闭成功! ", *para); 110 else 111 { 112 printf("server_%d ERROR:%d ", *para, wsa_res - WSABASEERR); 113 return -1; 114 } 115 SetEvent(event_end); 116 return 0; 117 } 118 119 int client_func(int* para) 120 { 121 printf("client在等待server运行... "); 122 // 等待server运行 123 while(WaitForSingleObject(event_server, 0) != 0) 124 Sleep(100);// 100ms的响应时间 125 126 int wsa_res = 0; 127 WSADATA wsa_data; 128 if(!(wsa_res = WSAStartup(0x0101, &wsa_data))) 129 printf("client_%d 打开成功! ", *para); 130 else 131 { 132 printf("client_%d ERROR:%d ", *para, wsa_res - WSABASEERR); 133 SetEvent(event_client); 134 return -1; 135 } 136 printf("client正在运行... "); 137 138 while(true) 139 { 140 int num; 141 scanf("%d", &num); 142 143 if(-1 == num) 144 { 145 printf("client结束... "); 146 SetEvent(event_client); 147 } 148 149 char recv_buf[100]; 150 char send_buf[100]; 151 152 itoa(num, send_buf, 10); 153 154 ////////////////////////////////////////////////////////////////////////// 155 156 SOCKET socket_client=socket(AF_INET,SOCK_STREAM,0); 157 SOCKADDR_IN addr_server; 158 addr_server.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); 159 addr_server.sin_family=AF_INET; 160 addr_server.sin_port=htons(6666); 161 connect(socket_client,(SOCKADDR*)&addr_server,sizeof(SOCKADDR)); 162 send(socket_client, send_buf,strlen(send_buf), 0); 163 if(0 < recv(socket_client,recv_buf,100,0) && -1 != num) 164 printf("client收到:%s ",recv_buf); 165 else if( -1 != num) 166 printf("client什么也没有收到。。。 "); 167 168 closesocket(socket_client); 169 170 if(-1 == num) 171 break; 172 173 ////////////////////////////////////////////////////////////////////////// 174 } 175 if(!(wsa_res = WSACleanup())) 176 printf("client_%d 关闭成功! ", *para); 177 else 178 { 179 printf("client_%d ERROR:%d ", *para, wsa_res - WSABASEERR); 180 SetEvent(event_client); 181 182 return -1; 183 } 184 185 return 0; 186 } 187 188 int main() 189 { 190 int i = 0; 191 int j = 1; 192 193 // 重置信号,默认状态下不需要重置 194 //ResetEvent(event_server); 195 //ResetEvent(event_client); 196 //ResetEvent(event_end); 197 198 Thread server_thread(server_func, &i); 199 Thread client_thread(client_func, &j); 200 server_thread.resume(); 201 client_thread.resume(); 202 203 // 等待server结束 204 while(WaitForSingleObject(event_end, 0) != 0) 205 Sleep(100);// 100ms的响应时间 206 printf("全部结束! "); 207 208 server_thread.terminate(); 209 client_thread.terminate(); 210 211 system("pause"); 212 return 0; 213 }
输出结果:
client在等待server运行... server_0 打开成功! server 准备完毕... server在等待client... client_1 打开成功! client正在运行... 2 server收到:2 client收到:2的平方是:4 32 server收到:32 client收到:32的平方是:1024 -1 client结束... server_0 关闭成功! client_1 关闭成功! 全部结束! 请按任意键继续. . .