仅为练手,可支持多个client连接。
/***************************************************************************************************** * File name: WSAOverlappedRoutine.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 /* It's a true good idea */ struct Operation_data_t { WSAOVERLAPPED wsa_overlapped; WSABUF wsa_buf; DWORD flags; DWORD transferred_bytes; char recv_buffer[50]; }; /***************************************************************************************************** * Global variables *****************************************************************************************************/ static HANDLE s_recv_thread_handle = INVALID_HANDLE_VALUE; static SOCKET s_accept_socket[64] = {INVALID_SOCKET}; static int s_accept_socket_cnt = 0; static Operation_data_t s_operation_data = {0}; /***************************************************************************************************** * Global function *****************************************************************************************************/ void CALLBACK CompleteRoutineIO(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags); DWORD WINAPI RunTask(LPVOID lp); DWORD WINAPI DoHandleRecv(LPVOID lp); int main() { WSADATA wsa_data; SOCKET listen_socket; int port = 27150; int ret_val; SOCKADDR_IN server_addr; BOOL is_start_thread = FALSE; /* 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; } is_start_thread = FALSE; s_recv_thread_handle = ::CreateThread(NULL, 0, RunTask, NULL, CREATE_SUSPENDED, NULL); if(INVALID_HANDLE_VALUE == s_recv_thread_handle) { trace("Create thread Error: %d/n", GetLastError ()); return 1; } for(;;) { s_accept_socket[s_accept_socket_cnt++] = accept(listen_socket, NULL, NULL); if(!is_start_thread) { ResumeThread(s_recv_thread_handle); is_start_thread = TRUE; } } WSACleanup(); system("pause"); return 0; } DWORD WINAPI RunTask(LPVOID lp) { DWORD ret; s_operation_data.wsa_buf.buf = s_operation_data.recv_buffer; s_operation_data.wsa_buf.len = sizeof(s_operation_data.recv_buffer); for(;;) { for(int loop = 0; loop < s_accept_socket_cnt; ++loop) { ret = WSARecv( s_accept_socket[loop], &s_operation_data.wsa_buf, 1, &s_operation_data.transferred_bytes, &s_operation_data.flags, &s_operation_data.wsa_overlapped, CompleteRoutineIO); SleepEx(1000, TRUE); } } } void CALLBACK CompleteRoutineIO(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags) { Operation_data_t* operation_data = (Operation_data_t*)lpOverlapped; if(0 != dwError || 0 != cbTransferred) { trace("%s/n", operation_data->wsa_buf.buf); } } /***************************************************************************************************** * Class declare ******************************************************************************************************/ /***************************************************************************************************** * Class definition *****************************************************************************************************/ /***********************************End of File*******************************************************/
Client,可在VS下启动多个调试进程,Server输出效果就不一样了。
/***************************************************************************************************** * 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); printf("Client send: %s/n", send_buffer); Sleep(10); if(send_index >= 100) { //break; } } closesocket(s); WSACleanup(); } /***************************************************************************************************** * Class declare ******************************************************************************************************/ /***************************************************************************************************** * Class definition *****************************************************************************************************/ /***********************************End of File*******************************************************/
posted on 2011-04-16 22:42 zengqh 阅读(226) 评论(0) 编辑 收藏 举报