重叠IO二

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

// Overlpped_Server2.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)); dwTrans = 0; dwFlags = 0; nOpType = OP_NULL; } public: WSAOVERLAPPED ol; SOCKET sock; SOCKADDR_IN addr; WSABUF wsaBuf; char szBuf[MAX_BUFFER]; DWORD dwTrans; DWORD dwFlags; OPERATIONINFO nOpType; }OVERLAPPEDINFO, *POVERLAPPEDINFO; unsigned __stdcall ThreadProc(LPVOID lParam); void CALLBACK CompleteRoutine(DWORD dwError, DWORD dwTrans, LPWSAOVERLAPPED Overlppad, DWORD dwFlags); BOOL bExitThread = FALSE; int g_nIndex = 0; OVERLAPPEDINFO* g_ol[WSA_MAXIMUM_WAIT_EVENTS] = {0}; int _tmain(int argc, _TCHAR* argv[]) { WSADATA wsaData; if(0 != WSAStartup(MAKEWORD(2, 2), &wsaData)) { printf("WSAStartup failed with error: %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 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(INVALID_SOCKET == sock) { printf("socket failed with error code: %d/n", WSAGetLastError()); WSACleanup(); return EXIT_FAILURE; } SOCKADDR_IN addr = {0}; addr.sin_family = AF_INET; addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); addr.sin_port = htons(5050); if(SOCKET_ERROR == bind(sock, (LPSOCKADDR)&addr, sizeof(addr))) { printf("bind failed with error code: %d/n", WSAGetLastError()); closesocket(sock); sock = INVALID_SOCKET; WSACleanup(); return EXIT_FAILURE; } if(SOCKET_ERROR == listen(sock, 5)) { printf("listen failed with error code: %d/n", WSAGetLastError()); closesocket(sock); sock = INVALID_SOCKET; WSACleanup(); return EXIT_FAILURE; } HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, NULL, 0, NULL); SOCKADDR_IN remote = {0}; int len = sizeof(remote); printf("Server start, wait for client to connect .../n"); while(TRUE) { SOCKET sNew = accept(sock, (LPSOCKADDR)&remote, &len); if(INVALID_SOCKET == sNew) { printf("accept failed with error code: %d/n", WSAGetLastError()); continue; } printf("Client

相关文章
|
6月前
|
缓存 Linux API
文件IO和标准IO的区别
文件IO和标准IO的区别
81 2
|
1月前
|
Java Linux 索引
IO的分类有哪些
IO的分类有哪些
|
1月前
|
数据挖掘 开发者
网络IO模型
【10月更文挑战第6天】网络IO模型
41 3
|
1月前
|
开发者
什么是面向网络的IO模型?
【10月更文挑战第6天】什么是面向网络的IO模型?
22 3
|
3月前
|
消息中间件 网络协议 Java
你不得不了解的网络IO模型知识
该文章主要讲述了网络I/O模型的相关知识,包括不同的I/O模型以及它们的特点和应用场景。
你不得不了解的网络IO模型知识
|
6月前
|
缓存 Java Unix
五种网络IO模型
前言   本文重点在于介绍五种网络IO模型
505 0
网络IO 多路IO复用 之 select
网络IO 多路IO复用 之 select
|
缓存 Java Linux
网络IO模型总结
网络IO模型总结
105 0
|
网络协议 Java Unix
四种IO模型详解下
《基础系列》
125 0