只能处理一个连接,练手而用。
/***************************************************************************************************** * File name: WSAOverlappedModel.cpp * Create time: 2011/04/16 * Module: View * Author: zengqh * Blog: http://blog.csdn.net/aimyton * --------------------------------------------------------------------------------------------------- * Memo: * *****************************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <WinSock2.h> #include <Windows.h> #pragma comment(lib, "WS2_32.lib") /***************************************************************************************************** * Macro and Struct definition *****************************************************************************************************/ #define trace printf /***************************************************************************************************** * Global variables *****************************************************************************************************/ /***************************************************************************************************** * Global function *****************************************************************************************************/ int main() { WSADATA wsa_data; SOCKET listen_socket; SOCKET accept_socket; int port = 27150; int ret_val; SOCKADDR_IN server_addr; WSAEVENT event_list[64]; int event_total = 0; int select_index; char recv_buffer[50] = {0}; WSAOVERLAPPED wsa_overlapped; WSABUF wsa_buf; DWORD recv_bytes = 0; DWORD flags = 0; DWORD transferred_bytes = 0; /* 1.Startup */ ret_val = WSAStartup(MAKEWORD(2, 2), &wsa_data); if(0 != ret_val) { trace("WSAStartup Error: %d/n", WSAGetLastError ()); return 1; } /* 2.Create socket */ listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(INVALID_SOCKET == listen_socket) { trace("socket Error: %d/n", WSAGetLastError ()); return 1; } /* 3.Bind */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ret_val = bind(listen_socket, (SOCKADDR*)&server_addr, sizeof(server_addr)); if(SOCKET_ERROR == ret_val) { trace("bind Error: %d/n", WSAGetLastError ()); return 1; } /* 4.Listen */ ret_val = listen(listen_socket, 5); if(SOCKET_ERROR == ret_val) { trace("listen Error: %d/n", WSAGetLastError ()); return 1; } accept_socket = accept(listen_socket, NULL, NULL); if(INVALID_SOCKET == accept_socket) { trace("accept Error: %d/n", WSAGetLastError ()); return 1;; } event_list[event_total] = WSACreateEvent(); memset(&wsa_overlapped, 0, sizeof(wsa_overlapped)); wsa_overlapped.hEvent = event_list[event_total]; wsa_buf.buf = recv_buffer; wsa_buf.len = sizeof(recv_buffer); ++event_total; /* First recv */ ret_val = WSARecv( accept_socket, &wsa_buf, 1, &recv_bytes, &flags, &wsa_overlapped, NULL); for(;;) { select_index = WSAWaitForMultipleEvents( event_total, event_list, FALSE, WSA_INFINITE, FALSE); WSAResetEvent(event_list[select_index - WSA_WAIT_EVENT_0]); BOOL bRet = WSAGetOverlappedResult( accept_socket, &wsa_overlapped, &transferred_bytes, FALSE, &flags); if(!bRet) { trace("WSAGetOverlappedResult Error: %d/n", WSAGetLastError ()); break; } if(0 == transferred_bytes) { break; } /* Handle recv data */ trace("%s/n", wsa_buf.buf); /* Next recv */ memset(&wsa_overlapped, 0, sizeof(wsa_overlapped)); wsa_overlapped.hEvent = event_list[select_index - WSA_WAIT_EVENT_0]; wsa_buf.buf = recv_buffer; wsa_buf.len = sizeof(recv_buffer); ret_val = WSARecv( accept_socket, &wsa_buf, 1, &recv_bytes, &flags, &wsa_overlapped, NULL); } WSACleanup(); system("pause"); } /***************************************************************************************************** * Class declare ******************************************************************************************************/ /***************************************************************************************************** * Class definition *****************************************************************************************************/ /***********************************End of File*******************************************************/
Client简单测试:
/***************************************************************************************************** * File name: Send.cpp * Create time: 2011/04/12 * Module: View * Author: zengqh * Blog: http://blog.csdn.net/aimyton * --------------------------------------------------------------------------------------------------- * Memo: * *****************************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <Windows.h> #pragma comment(lib, "WS2_32.lib") /***************************************************************************************************** * Macro and Struct definition *****************************************************************************************************/ #define trace printf /***************************************************************************************************** * Global variables *****************************************************************************************************/ /***************************************************************************************************** * Global function *****************************************************************************************************/ int main() { WSADATA wsa_data; SOCKET s; SOCKADDR_IN server_addr; int port = 27150; int ret_val; char send_buffer[50] = {0}; static int send_index = 0; /* 1.Startup */ ret_val = WSAStartup(MAKEWORD(2, 2), &wsa_data); if(0 != ret_val) { trace("WSAStartup Error: %d/n", WSAGetLastError ()); return 1; } s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(INVALID_SOCKET == s) { trace("socket Error: %d/n", WSAGetLastError ()); return 1; } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ret_val = connect(s, (SOCKADDR*)&server_addr, sizeof(server_addr)); if(SOCKET_ERROR == ret_val) { trace("connect Error: %d/n", WSAGetLastError ()); return 1; } int send_bytes = 0; for(;;) { sprintf(send_buffer, "Send, %d", send_index++); send_bytes = send(s, send_buffer, sizeof(send_buffer), 0); Sleep(10); if(send_index >= 100) { //break; } } closesocket(s); WSACleanup(); } /***************************************************************************************************** * Class declare ******************************************************************************************************/ /***************************************************************************************************** * Class definition *****************************************************************************************************/ /***********************************End of File*******************************************************/
posted on 2011-04-16 19:19 zengqh 阅读(257) 评论(0) 编辑 收藏 举报