写的一个练习程序。
Server:
/***************************************************************************************************** * File name: WSAEventSelect.cpp * Create time: 2011/04/12 * 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; SOCKET socket_list[64]; WSANETWORKEVENTS network_events; int select_index; char recv_buffer[50] = {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; } WSAEVENT accept_event = WSACreateEvent(); ret_val = WSAEventSelect(listen_socket, accept_event, FD_ACCEPT | FD_CLOSE); if(SOCKET_ERROR == ret_val) { trace("WSAEventSelect Error: %d/n", WSAGetLastError ()); return 1; } socket_list[event_total] = listen_socket; event_list[event_total++] = accept_event; /* 4.Listen */ ret_val = listen(listen_socket, 5); if(SOCKET_ERROR == ret_val) { trace("listen Error: %d/n", WSAGetLastError ()); return 1; } for(;;) { select_index = WSAWaitForMultipleEvents(event_total, event_list, FALSE, WSA_INFINITE, FALSE); if(WSA_WAIT_FAILED == select_index) { trace("WSAWaitForMultipleEvents Error: %d/n", WSAGetLastError ()); break; } ret_val = WSAEnumNetworkEvents( socket_list[select_index - WSA_WAIT_EVENT_0], event_list[select_index - WSA_WAIT_EVENT_0], &network_events ); if(SOCKET_ERROR == ret_val) { trace("WSAEnumNetworkEvents Error: %d/n", WSAGetLastError ()); break; } /* Accept event */ if(network_events.lNetworkEvents & FD_ACCEPT) { if(0 != network_events.iErrorCode[FD_ACCEPT_BIT]) { trace("iErrorCode Error: %d/n", WSAGetLastError ()); break; } accept_socket = accept(socket_list[select_index - WSA_WAIT_EVENT_0], NULL, NULL); if(INVALID_SOCKET == accept_socket) { trace("accept Error: %d/n", WSAGetLastError ()); break; } WSAEVENT new_event = WSACreateEvent(); ret_val = WSAEventSelect(accept_socket, new_event, FD_READ | FD_WRITE); if(SOCKET_ERROR == ret_val) { trace("WSAEventSelect Error: %d/n", WSAGetLastError ()); return 1; } socket_list[event_total] = accept_socket; event_list[event_total++] = new_event; } /* Read event */ if(network_events.lNetworkEvents & FD_READ) { if(0 != network_events.iErrorCode[FD_READ_BIT]) { trace("iErrorCode Error: %d/n", WSAGetLastError ()); break; } int recv_bytes = recv(accept_socket, recv_buffer, sizeof(recv_buffer), 0); trace("%s/n", recv_buffer); } /* Close event */ if(network_events.lNetworkEvents & FD_CLOSE) { if(0 != network_events.iErrorCode[FD_CLOSE_BIT]) { trace("iErrorCode Error: %d/n", WSAGetLastError ()); break; } closesocket(socket_list[select_index - WSA_WAIT_EVENT_0]); } } 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-12 22:55 zengqh 阅读(182) 评论(0) 编辑 收藏 举报