重叠IO

简介:  // NetWork.cpp : Defines the entry point for the console application.

 

// NetWork.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Winsock2.h> #include <Windows.h> #include <process.h> #pragma comment(lib, "WS2_32.lib") #define MAX_BUFFER 256 #define MAX_TIMEOUT 1000 typedef enum _OPERATION_INFO { OP_NULL, OP_READ, OP_WRITE }OPERATIONINFO; typedef struct _OVERLAPPED_INFO_ { public: _OVERLAPPED_INFO_() { Clean(); } ~_OVERLAPPED_INFO_() { Clean(); } protected: void Clean() { ol.hEvent = NULL; sock = INVALID_SOCKET; memset(&addr, 0, sizeof(addr)); addr.sin_addr.S_un.S_addr = INADDR_ANY; addr.sin_family = AF_INET; addr.sin_port = htons(0); wsaBuf.buf = szBuf; wsaBuf.len = MAX_BUFFER; memset(szBuf, 0, sizeof(szBuf)); nOpType = OP_NULL; } public: WSAOVERLAPPED ol; SOCKET sock; SOCKADDR_IN addr; WSABUF wsaBuf; char szBuf[MAX_BUFFER]; OPERATIONINFO nOpType; }OVERLAPPEDINFO, *POVERLAPPEDINFO; WSAEVENT g_event[WSA_MAXIMUM_WAIT_EVENTS] = {0}; OVERLAPPEDINFO* g_ol[WSA_MAXIMUM_WAIT_EVENTS] = {0}; int g_nIndex = 0; BOOL g_bExitThread = FALSE; unsigned __stdcall ThreadProc(LPVOID lParam); int _tmain(int argc, _TCHAR* argv[]) { WSADATA wsaData; if(0 != WSAStartup(MAKEWORD(2, 2), &wsaData)) { printf("WSAStartup failed with error code: %d/n", GetLastError()); return EXIT_FAILURE; } if(2 != HIBYTE(wsaData.wVersion) || 2 != LOBYTE(wsaData.wVersion)) { printf("Socket version not supported./n"); WSACleanup(); return EXIT_FAILURE; } SOCKET sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(INVALID_SOCKET == sListen) { printf("socket failed with error code: %d/n", WSAGetLastError()); WSACleanup(); return EXIT_FAILURE; } SOCKADDR_IN addr = {0}; addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); addr.sin_family = AF_INET; addr.sin_port = htons(5050); if(SOCKET_ERROR == bind(sListen, (LPSOCKADDR)&addr, sizeof(addr))) { printf("bind failed with error code: %d/n", WSAGetLastError()); closesocket(sListen); sListen = INVALID_SOCKET; WSACleanup(); return EXIT_FAILURE; } if(SOCKET_ERROR == listen(sListen, 5)) { printf("listen failed with error code: %d/n", WSAGetLastError()); closesocket(sListen); sListen = INVALID_SOCKET; WSACleanup(); return EXIT_FAILURE; } HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, NULL, 0, NULL); if(NULL == hThread) { printf("_beginthreadex failed with error code: %d/n", GetLastError()); closesocket(sListen); sListen = INVALID_SOCKET; WSACleanup(); return EXIT_FAILURE; } printf("Server start listen and wait for client to connect .../n"); SOCKADDR_IN remote = {0}; int len = sizeof(remote); while(TRUE) { if(g_nIndex >= WSA_MAXIMUM_WAIT_EVENTS) { printf("Too many connect, wait for disconnect .../n"); continue; } SOCKET sNew = accept(sListen, (LPSOCKADDR)&remote, &len); if(INVALID_SOCKET == sNew) { printf("accept failed with error code: %d/n", WSAGetLastError()); continue; } printf("<IP: %s, Port: %d> client come in./n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port)); DWORD dwTrans = 0; DWORD dwFlags = 0; g_ol[g_nIndex] = new OVERLAPPEDINFO; g_ol[g_nIndex]->sock = sNew; memcpy(&g_ol[g_nIndex]->addr, &remote, sizeof(remote)); g_event[g_nIndex] = g_ol[g_nIndex]->ol.hEvent = WSACreateEvent(); g_ol[g_nIndex]->nOpType = OP_READ; if(SOCKET_ERROR == WSARecv(g_ol[g_nIndex]->sock, &(g_ol[g_nIndex]->wsaBuf), 1, &dwTrans, &dwFlags, (LPWSAOVERLAPPED)&(g_ol[g_nIndex]->ol), NULL)) { if(ERROR_IO_PENDING != WSAGetLastError()) { printf("WSARecv failed with error code: %d/n", WSAGetLastError()); closesocket(g_ol[g_nIndex]->sock); delete g_ol[g_nIndex]; g_ol[g_nIndex] = NULL; continue; } } g_nIndex++; } // End of thread and exit process g_bExitThread = TRUE; // Exit thread. WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); WSACleanup(); return 0; } unsigned __stdcall ThreadProc(LPVOID lParam) { while(!g_bExitThread) { DWORD dwTrans = 0; DWORD dwFlags = 0; DWORD dwIndex = WSAWaitForMultipleEvents(g_nIndex, g_event, FALSE, MAX_TIMEOUT, FALSE); if(WSA_WAIT_FAILED == dwIndex || WSA_WAIT_TIMEOUT == dwIndex) { continue; } dwIndex -= WSA_WAIT_EVENT_0; for(int i = dwIndex; i < g_nIndex; i++) { dwIndex = WSAWaitForMultipleEvents(1, &g_event[dwIndex], TRUE, INFINITE, FALSE); if(WSA_WAIT_FAILED == dwIndex) { continue; } dwIndex -= WSA_WAIT_EVENT_0; WSAResetEvent(g_event[dwIndex]); BOOL bRet = WSAGetOverlappedResult(g_ol[dwIndex]->sock, &(g_ol[dwIndex]->ol), &dwTrans, FALSE, &dwFlags); if(bRet) { if(dwTrans > 0) { switch(g_ol[dwIndex]->nOpType) { case OP_READ: { printf("recv client

目录
相关文章
Socket重叠IO
1.为什么到现在才弄懂这个 不知道这个Socket重叠IO这种模型是不是socket IO完成端口的基础,不过我感觉,学习一下这个再去学习socket IO完成端口是比较有好处的。 这个Scoket重叠IO我以前记得看过好几次,都没看懂。
811 0
重叠IO二
// Overlpped_Server2.cpp : Defines the entry point for the console application.
613 0
|
3月前
|
存储 Java
【IO面试题 四】、介绍一下Java的序列化与反序列化
Java的序列化与反序列化允许对象通过实现Serializable接口转换成字节序列并存储或传输,之后可以通过ObjectInputStream和ObjectOutputStream的方法将这些字节序列恢复成对象。
|
4月前
|
Java 大数据
解析Java中的NIO与传统IO的区别与应用
解析Java中的NIO与传统IO的区别与应用
|
2月前
|
Java 大数据 API
Java 流(Stream)、文件(File)和IO的区别
Java中的流(Stream)、文件(File)和输入/输出(I/O)是处理数据的关键概念。`File`类用于基本文件操作,如创建、删除和检查文件;流则提供了数据读写的抽象机制,适用于文件、内存和网络等多种数据源;I/O涵盖更广泛的输入输出操作,包括文件I/O、网络通信等,并支持异常处理和缓冲等功能。实际开发中,这三者常结合使用,以实现高效的数据处理。例如,`File`用于管理文件路径,`Stream`用于读写数据,I/O则处理复杂的输入输出需求。
|
3月前
|
Java 数据处理
Java IO 接口(Input)究竟隐藏着怎样的神秘用法?快来一探究竟,解锁高效编程新境界!
【8月更文挑战第22天】Java的输入输出(IO)操作至关重要,它支持从多种来源读取数据,如文件、网络等。常用输入流包括`FileInputStream`,适用于按字节读取文件;结合`BufferedInputStream`可提升读取效率。此外,通过`Socket`和相关输入流,还能实现网络数据读取。合理选用这些流能有效支持程序的数据处理需求。
42 2
|
3月前
|
XML 存储 JSON
【IO面试题 六】、 除了Java自带的序列化之外,你还了解哪些序列化工具?
除了Java自带的序列化,常见的序列化工具还包括JSON(如jackson、gson、fastjson)、Protobuf、Thrift和Avro,各具特点,适用于不同的应用场景和性能需求。
|
3月前
|
缓存 Java
【IO面试题 一】、介绍一下Java中的IO流
Java中的IO流是对数据输入输出操作的抽象,分为输入流和输出流,字节流和字符流,节点流和处理流,提供了多种类支持不同数据源和操作,如文件流、数组流、管道流、字符串流、缓冲流、转换流、对象流、打印流、推回输入流和数据流等。
【IO面试题 一】、介绍一下Java中的IO流
|
4月前
|
存储 缓存 Java
Java零基础入门之IO流详解(二)
Java零基础入门之IO流详解(二)
|
4月前
|
Java 大数据
解析Java中的NIO与传统IO的区别与应用
解析Java中的NIO与传统IO的区别与应用