实验过程:
1.Server等待连接到来,Client连接进入
2.某线程处理Client的连接请求,完成后在对应socket上投3个WSASend
发送内容"nihao from + 当前线程ID + "
3.WSASend完成后在完成队列投放IO完成包,任何获得完成包的线程
再投递3个"nihao from + 当前线程ID + "
4.Client申请10M堆空间,待空间填满后将内容以二进制形式写入文件1.txt
实验结果:
1.txt中规规整整的9.99M的
实验结论:
在同一socket上同时投递发送,A、B、C、D、E发送顺序不能保证(因为多线程被调用顺序不能预测,故投递的顺序不能保证),
但单次投递数据保证完整,即A的数据不会被B、C、D、E干扰
实验客户端:
Client_IOCP_SimultaneousSend
实验代码:
#include <WinSock2.h> #include <Windows.h> #include <iostream> #include <process.h> #include <string> #include <MSWSock.h> #include <set> #include "autoLock.h" #pragma comment(lib, "Ws2_32.lib") #pragma comment(lib, "Kernel32.lib") #pragma comment(lib, "Mswsock.lib") #define BUF_LEN 1024 enum OperateType { OP_RECV, OP_SEND, OP_ACCEPT, }; typedef struct PER_HANDLE_DATA { SOCKET s; SOCKADDR_IN addr; /*参考性添加, 防止重复delete PER_HANDLE_DATA, 目前没有找到更好的办法*/ HANDLE mutex; bool flag; }PER_HANDLE_DATA, *LPPER_HANDLE_DATA; typedef struct PER_IO_DATA { OVERLAPPED overlapped; SOCKET cs; int no; char buf[BUF_LEN]; int operationType; }PER_IO_DATA, *LPPER_IO_DATA; SOCKET SocketInitBindListen() { SOCKET s = socket(AF_INET, SOCK_STREAM, 0); if(INVALID_SOCKET == s) { std::cout<<"create socket failed : "<<GetLastError()<<std::endl; return INVALID_SOCKET; } SOCKADDR_IN addr; addr.sin_family = AF_INET; addr.sin_addr.S_un.S_addr = INADDR_ANY; addr.sin_port = htons(4444); int ret = bind(s, (sockaddr*)&addr, sizeof(addr)); if(SOCKET_ERROR == ret) { std::cout<<"bind failed : "<<GetLastError()<<std::endl; return SOCKET_ERROR; } ret = listen(s, 10); if(SOCKET_ERROR == s) { std::cout<<"listen fail : "<<GetLastError()<<std::endl; return SOCKET_ERROR; } return s; } bool PostAccept(SOCKET listenSocket) { SOCKET cs = socket(AF_INET, SOCK_STREAM, 0); if(INVALID_SOCKET == cs) { std::cout<<"Create Socket Failed : "<<GetLastError()<<std::endl; return false; } LPPER_IO_DATA ppiod = new PER_IO_DATA; ZeroMemory(&(ppiod->overlapped), sizeof(OVERLAPPED)); ppiod->operationType = OP_ACCEPT; ppiod->cs = cs; DWORD dwRecv; int len = sizeof(sockaddr_in) + 16; bool ret = AcceptEx(listenSocket, ppiod->cs, ppiod->buf, 0, len, len, &dwRecv, &ppiod->overlapped); if(false == ret && ERROR_IO_PENDING != GetLastError()) { std::cout<<"AcceptEx Failed : "<<GetLastError()<<std::endl; return false; } return true; } bool PostSend(SOCKET s, const char *buf, int len) { LPPER_IO_DATA ppiod = new PER_IO_DATA; ZeroMemory(&(ppiod->overlapped), sizeof(OVERLAPPED)); ppiod->operationType = OP_SEND; memset(ppiod->buf, 0, BUF_LEN); memcpy(ppiod->buf, buf, len); WSABUF databuf; databuf.buf = ppiod->buf; databuf.len = len; DWORD dwRecv = 0; DWORD dwFlags = 0; int ret = WSASend(s, &databuf, 1, &dwRecv, dwFlags, &ppiod->overlapped, NULL); if(SOCKET_ERROR == ret && WSA_IO_PENDING != GetLastError()) return false; return true; } bool PostRecv(SOCKET s, int n) { LPPER_IO_DATA ppiod = new PER_IO_DATA; ZeroMemory(&(ppiod->overlapped), sizeof(OVERLAPPED)); ppiod->operationType = OP_RECV; ppiod->no = n; memset(ppiod->buf, 0, BUF_LEN); WSABUF databuf; databuf.buf = ppiod->buf; databuf.len = BUF_LEN; DWORD dwRecv = 0; DWORD dwFlags = 0; int ret = WSARecv(s, &databuf, 1, &dwRecv, &dwFlags, &ppiod->overlapped, NULL); if(SOCKET_ERROR == ret && WSA_IO_PENDING != GetLastError()) return false; return true; } unsigned int __stdcall Func(void *arg) { SOCKET s = (SOCKET)arg; PostRecv(s, 2); while(std::cin.get()) { std::string str = "nihaihaoma"; bool ret = PostSend(s, str.c_str(), str.length()); if(false == ret) break; } _endthreadex(0); return 0; } unsigned int __stdcall ThreadFunc(void *arg) { HANDLE hcp = (HANDLE)arg; if(NULL == hcp) { std::cout<<"thread arg error"<<std::endl; return -1; } DWORD dwNum = 0; LPPER_HANDLE_DATA pphd; LPPER_IO_DATA ppiod; while(true) { bool ret = GetQueuedCompletionStatus(hcp, &dwNum, (LPDWORD)&pphd, (LPOVERLAPPED*)&ppiod, WSA_INFINITE); //线程退出控制,没有释放申请的堆空间,还不完善 if(-1 == dwNum) { std::cout<<"Thread Exit"<<std::endl; _endthreadex(0); return 0; } //连接断开 int type = ppiod->operationType; if(0 == dwNum && OP_ACCEPT != type) { std::cout<<"The Connection Be Closed : "<<GetLastError()<<std::endl; //在一个socket上投递多个WSARecv需要考虑连接被Client断开时所有异步WSARecv均返回不会重复delete PER_HANDLE_DATA AutoLock lock(pphd->mutex); if(true == pphd->flag) { closesocket(pphd->s); delete pphd; pphd->flag = false; } delete ppiod; continue; } //错误发生 if(false == ret) { std::cout<<"An Error Occurs : "<<GetLastError()<<std::endl; if(pphd != NULL) { closesocket(pphd->s); delete pphd; } delete ppiod; continue; } if(OP_RECV == type) { // std::cout<<"接收完成"<<std::endl; std::cout<<"接收端口号 :"<<pphd->s<<std::endl; // ppiod->buf[dwNum] = ' '; std::cout<<"Receiver : "<<ppiod->no<<" "<<ppiod->buf<<std::endl; ZeroMemory(&(ppiod->overlapped), sizeof(OVERLAPPED)); ZeroMemory(ppiod->buf, BUF_LEN); WSABUF databuf; databuf.buf = ppiod->buf; databuf.len = BUF_LEN; DWORD dwRecv = 0; DWORD dwFlags = 0; WSARecv(pphd->s, &databuf, 1, &dwRecv, &dwFlags, &ppiod->overlapped, NULL); } else if(OP_SEND == type) { SOCKET s = pphd->s; ZeroMemory(&(ppiod->overlapped), sizeof(OVERLAPPED)); ppiod->operationType = OP_SEND; std::string str = "nihao from " + std::to_string((ULONGLONG)GetCurrentThreadId()) + " "; memcpy(ppiod->buf, str.c_str(), str.length()); WSABUF databuf; databuf.buf = ppiod->buf; databuf.len = str.length(); DWORD dwRecv = 0; DWORD dwFlags = 0; int ret = WSASend(s, &databuf, 1, &dwRecv, dwFlags, &ppiod->overlapped, NULL); if(SOCKET_ERROR == ret && WSA_IO_PENDING != GetLastError()) return false; PostSend(s, str.c_str(), str.length()); PostSend(s, str.c_str(), str.length()); } else if(OP_ACCEPT == type) { // std::cout<<"连接完成"<<std::endl; // SOCKET cs = ppiod->cs; int len = sizeof(sockaddr_in) + 16; int localLen, remoteLen; LPSOCKADDR localAddr, remoteAddr; GetAcceptExSockaddrs(ppiod->buf, 0, len, len, (SOCKADDR **)&localAddr, &localLen, (SOCKADDR **)&remoteAddr, &remoteLen); LPPER_HANDLE_DATA p = new PER_HANDLE_DATA; p->s = cs; memcpy(&p->addr, remoteAddr, remoteLen); char *ch = inet_ntoa(p->addr.sin_addr); p->mutex = CreateMutex(NULL, false, NULL); p->flag = true; CreateIoCompletionPort((HANDLE)cs, hcp, (DWORD)p, 0); ZeroMemory(&(ppiod->overlapped), sizeof(OVERLAPPED)); ppiod->operationType = OP_SEND; std::string str = "nihao from " + std::to_string((ULONGLONG)GetCurrentThreadId()) + " "; memcpy(ppiod->buf, str.c_str(), str.length()); WSABUF databuf; databuf.buf = ppiod->buf; databuf.len = str.length(); DWORD dwRecv = 0; DWORD dwFlags = 0; int ret = WSASend(cs, &databuf, 1, &dwRecv, dwFlags, &ppiod->overlapped, NULL); if(SOCKET_ERROR == ret && WSA_IO_PENDING != GetLastError()) return false; PostSend(cs, str.c_str(), str.length()); PostSend(cs, str.c_str(), str.length()); } } return 0; } int main() { WSADATA ws; if(WSAStartup(MAKEWORD(2, 2), &ws) != 0) { std::cout<<"WSAStartup error : "<<GetLastError()<<std::endl; return -1; } HANDLE hcp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); if(NULL == hcp) { std::cout<<"create completion port failed : "<<GetLastError()<<std::endl; return -1; } std::set<HANDLE> setWorkers; SYSTEM_INFO si; GetSystemInfo(&si); for(int i = 0; i < si.dwNumberOfProcessors * 2 + 2; i++) { HANDLE worker = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, (LPVOID)hcp, 0, NULL); if(NULL == worker) { std::cout<<"create thread failed : "<<GetLastError()<<std::endl; return -1; } setWorkers.insert(worker); } SOCKET s = SocketInitBindListen(); if(INVALID_SOCKET == s) { std::cout<<"socket init failed"<<std::endl; return -1; } LPPER_HANDLE_DATA pphd = new PER_HANDLE_DATA; pphd->s = s; CreateIoCompletionPort((HANDLE)s, hcp, (DWORD)pphd, 0); bool ret = PostAccept(s); if(false == ret) { std::cout<<"PostAccept Failed"<<std::endl; return -1; } //退出控制 /*std::cin.get(); for(int i = 0; i < setWorkers.size(); i++) PostQueuedCompletionStatus(hcp, -1, NULL, NULL);*/ auto iter = setWorkers.begin(); for(; iter != setWorkers.end(); iter++) WaitForSingleObject(*iter, INFINITE); WSACleanup(); std::cin.get(); return 0; }
Client代码
#include <WinSock2.h> #include <Windows.h> #include <iostream> #include <fstream> #include <string> #include <process.h> #pragma comment(lib, "Ws2_32.lib") int main() { WSADATA ws; if(WSAStartup(MAKEWORD(2, 2), &ws) != 0) return false; SOCKET s = socket(AF_INET, SOCK_STREAM, 0); if(INVALID_SOCKET == s) return false; sockaddr_in addr = {0}; addr.sin_addr.S_un.S_addr = inet_addr("192.168.4.18"); addr.sin_family = AF_INET; addr.sin_port = htons(4444); int ret = connect(s, (sockaddr*)&addr, sizeof(addr)); if(SOCKET_ERROR == ret) { std::cout<<"Connect Server Failed : "<<GetLastError()<<std::endl; return -1; } { int maxSize = 10*1024*1024; char *chmax = new char[maxSize]; int cursor = 0, ret = 0; char ch[100*1024] = {0}; while(cursor+ret < maxSize) { memcpy(chmax+cursor, ch, ret); cursor += ret; ret = recv(s, ch, 100*1024, 0); if(SOCKET_ERROR == ret) { std::cout<<"Recv Data Failed : "<<GetLastError()<<std::endl; return -1; } if(0 == ret) break; } std::ofstream os("C:\Documents and Settings\Administrator\桌面\1.txt", std::ios::binary); os.write(chmax, cursor); os.close(); } std::cout<<"over"<<std::endl; std::cin.get(); return 0; }