【c++】Winsock I/O模型
select模型
////////////////////////////////////////////////////// // select.cpp文件 #include "InitSock.h" #include <stdio.h> #include <windows.h> CInitSock theSock; // 初始化Winsock库 int main() { USHORT nPort = 4567; // 此服务器监听的端口号 // 创建监听套节字 SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(nPort); sin.sin_addr.S_un.S_addr = INADDR_ANY; // 绑定套节字到本地机器 if (::bind(sListen, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR) { printf(" Failed bind() \n"); return -1; } // 进入监听模式 ::listen(sListen, 5); // select模型处理过程 // 1)初始化一个套节字集合fdSocket,添加监听套节字句柄到这个集合 fd_set fdSocket; // 所有可用套节字集合 FD_ZERO(&fdSocket); FD_SET(sListen, &fdSocket); while (TRUE) { // 2)将fdSocket集合的一个拷贝fdRead传递给select函数, // 当有事件发生时,select函数移除fdRead集合中没有未决I/O操作的套节字句柄,然后返回。 fd_set fdRead = fdSocket; int nRet = ::select(0, &fdRead, NULL, NULL, NULL); if (nRet > 0) { // 3)通过将原来fdSocket集合与select处理过的fdRead集合比较, // 确定都有哪些套节字有未决I/O,并进一步处理这些I/O。 for (int i = 0; i < (int)fdSocket.fd_count; i++) { if (FD_ISSET(fdSocket.fd_array[i], &fdRead)) { if (fdSocket.fd_array[i] == sListen) // (1)监听套节字接收到新连接 { if (fdSocket.fd_count < FD_SETSIZE) { sockaddr_in addrRemote; int nAddrLen = sizeof(addrRemote); SOCKET sNew = ::accept(sListen, (SOCKADDR*)&addrRemote, &nAddrLen); FD_SET(sNew, &fdSocket); char dst[16]; inet_ntop(AF_INET, (void*)&addrRemote.sin_addr, dst, 16); printf("接收到连接(%s)\n", dst); } else { printf(" Too much connections! \n"); continue; } } else { char szText[256]; int cnt = 0; string ans; while (cnt == 0 || cnt <= ans[0]) { int nRecv = ::recv(fdSocket.fd_array[i], szText, strlen(szText), 0); if (nRecv > 0) // (2)可读 { cnt += nRecv; szText[nRecv] = '\0'; ans += szText; printf("接收到包:%s \n", szText); cout << "ans:" << ans << endl; } else // (3)连接关闭、重启或者中断 { ::closesocket(fdSocket.fd_array[i]); printf("connection break"); FD_CLR(fdSocket.fd_array[i], &fdSocket); break; } } if(cnt>0) printf("接收到数据:%s \n", ans.c_str()+1); } } } } else { printf(" Failed select() \n"); break; } } return 0; }
#include <stdio.h> #include "InitSock.h" CInitSock initSock; // 初始化Winsock库 int main() { // 创建套节字 SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (s == INVALID_SOCKET) { printf(" Failed socket() \n"); return 0; } // 也可以在这里调用bind函数绑定一个本地地址 // 否则系统将会自动安排 // 填写远程地址信息 sockaddr_in servAddr; servAddr.sin_family = AF_INET; servAddr.sin_port = htons(4567); const char src[] = "127.0.0.1"; in_addr st; inet_pton(AF_INET, src, (void*)&st); servAddr.sin_addr = st; if (::connect(s, (sockaddr*)&servAddr, sizeof(servAddr)) == -1) { printf(" Failed connect() \n"); return 0; } while (true) { // 接收数据 string temp; while (true) { cin >> temp; string buff= char(strlen(temp.c_str())) + temp; send(s, buff.c_str(), strlen(buff.c_str()), 0); } } // 关闭套节字 ::closesocket(s); return 0; }
WSAAsyncSelect
/////////////////////////////////////////////// // WSAAsyncSelect.cpp文件 #include "InitSock.h" #include <stdio.h> #define WM_SOCKET WM_USER + 101 // 自定义消息 CInitSock theSock; LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int main() { wchar_t szClassName[] = TEXT("MainWClass"); WNDCLASSEX wndclass; // 用描述主窗口的参数填充WNDCLASSEX结构 wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WindowProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = NULL; wndclass.hIcon = ::LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = ::LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szClassName; wndclass.hIconSm = NULL; ::RegisterClassEx(&wndclass); // 创建主窗口 HWND hWnd = ::CreateWindowEx( 0, szClassName, TEXT(""), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL); if (hWnd == NULL) { wchar_t wchar1[] = TEXT("创建窗口出错!"); wchar_t wchar2[] = TEXT("error"); ::MessageBox(NULL, wchar1, wchar2, MB_OK); return -1; } USHORT nPort = 4567; // 此服务器监听的端口号 // 创建监听套节字 SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(nPort); sin.sin_addr.S_un.S_addr = INADDR_ANY; // 绑定套节字到本地机器 if (::bind(sListen, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR) { printf(" Failed bind() \n"); return -1; } // 将套接字设为窗口通知消息类型。 ::WSAAsyncSelect(sListen, hWnd, WM_SOCKET, FD_ACCEPT | FD_CLOSE); // 进入监听模式 ::listen(sListen, 5); // 从消息队列中取出消息 MSG msg; while (::GetMessage(&msg, NULL, 0, 0)) { // 转化键盘消息 ::TranslateMessage(&msg); // 将消息发送到相应的窗口函数 ::DispatchMessage(&msg); } // 当GetMessage返回0时程序结束 return msg.wParam; } LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_SOCKET: { // 取得有事件发生的套节字句柄 SOCKET s = wParam; // 查看是否出错 if (WSAGETSELECTERROR(lParam)) { ::closesocket(s); return 0; } // 处理发生的事件 switch (WSAGETSELECTEVENT(lParam)) { case FD_ACCEPT: // 监听中的套接字检测到有连接进入 { sockaddr_in clientaddr; char buff[16]; int addrlen = sizeof(clientaddr); SOCKET client = ::accept(s, (sockaddr*)&clientaddr, &addrlen); inet_ntop(AF_INET, (void*)&clientaddr.sin_addr, buff, sizeof(buff)); ::WSAAsyncSelect(client, hWnd, WM_SOCKET, FD_READ | FD_WRITE | FD_CLOSE); printf("user connect:%s", buff); cout << "端口为:" << clientaddr.sin_port; } break; case FD_WRITE: { } break; case FD_READ: { char szText[1024] = { 0 }; if (::recv(s, szText, 1024, 0) == -1) ::closesocket(s); else printf("接收数据:%s", szText); } break; case FD_CLOSE: { ::closesocket(s); } break; } } return 0; case WM_DESTROY: ::PostQuitMessage(0); return 0; } // 将我们不处理的消息交给系统做默认处理 return ::DefWindowProc(hWnd, uMsg, wParam, lParam); }