• C++ CompletionPort(完成端口)示例


    ECHO客户端

    #include <WINSOCK2.H>
    #include <stdio.h>
    #define SERVER_ADDRESS		"127.0.0.1"		//服务器地址
    #define PORT				5150			//端口
    #define MSGSIZE				1024			//信息缓冲大小
    
    #pragma comment(lib, "ws2_32.lib")
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	WSADATA     wsaData;
    	SOCKET      sClient;
    	SOCKADDR_IN server;
    	char        szMessage[MSGSIZE];
    	int         ret;
    
    	// Initialize Windows socket library
    	WSAStartup(0x0202, &wsaData);
    
    	// Create client socket
    	sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    	// Connect to server
    	memset(&server, 0, sizeof(SOCKADDR_IN));
    	server.sin_family = AF_INET;
    	server.sin_addr.S_un.S_addr = inet_addr(SERVER_ADDRESS);
    	server.sin_port = htons(PORT);
    
    	connect(sClient, (struct sockaddr *)&server, sizeof(SOCKADDR_IN));
    	printf("client is contect
    ");
    	while (TRUE)
    	{
    		printf("Send:");
    		gets(szMessage);
    
    		// Send message
    		send(sClient, szMessage, strlen(szMessage), 0);
    
    		// Receive message
    		ret = recv(sClient, szMessage, MSGSIZE, 0);
    		szMessage[ret] = '';
    
    		printf("Received [%d bytes]: '%s'
    ", ret, szMessage);
    	}
    
    	// Clean up
    	closesocket(sClient);
    	WSACleanup();
    	return 0;
    }
    

      Echo服务端代码:

    #include <WINSOCK2.H>
    #include <stdio.h>
    
    #define PORT				5150			//端口
    #define MSGSIZE				1024			//信息缓冲大小
    #pragma comment(lib, "ws2_32.lib")
    
    typedef enum
    {
    	RECV_POSTED
    }OPERATION_TYPE;
    
    typedef struct
    {
    	WSAOVERLAPPED  overlap;
    	WSABUF         Buffer;
    	char           szMessage[MSGSIZE];
    	DWORD          NumberOfBytesRecvd;
    	DWORD          Flags;
    	OPERATION_TYPE OperationType;
    }COverLapped, *p_COverLapped;
    
    DWORD WINAPI WorkerThread(LPVOID);
    
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	WSADATA                 wsaData;
    	SOCKET                  m_wListen = INVALID_SOCKET;		//监听句柄
    	SOCKET					m_wSocket = INVALID_SOCKET;		//
    	SOCKADDR_IN             local, client;
    	DWORD                   i, dwThreadId;
    	int                     iaddrSize = sizeof(SOCKADDR_IN);
    	HANDLE                  CompletionPort = INVALID_HANDLE_VALUE;
    	SYSTEM_INFO             systeminfo;
    	p_COverLapped			pOverLapped = NULL;
    
    	// Initialize Windows Socket library
    	WSAStartup(0x0202, &wsaData);
    
    	// Create completion port
    	CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
    
    	// Create worker thread
    	GetSystemInfo(&systeminfo);
    	for (i = 0; i < systeminfo.dwNumberOfProcessors; i++)
    	{
    		CreateThread(NULL, 0, WorkerThread, CompletionPort, 0, &dwThreadId);
    	}
    
    	// Create listening socket
    	m_wListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    	// Bind
    	local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    	local.sin_family = AF_INET;
    	local.sin_port = htons(PORT);
    	bind(m_wListen, (struct sockaddr *)&local, sizeof(SOCKADDR_IN));
    
    	// Listen
    	listen(m_wListen, 3);
    
    	while (TRUE)
    	{
    		// Accept a connection
    		m_wSocket = accept(m_wListen, (struct sockaddr *)&client, &iaddrSize);
    		printf("Accepted client:%s:%d
    ", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
    
    		//初始化对象
    		//CSocketItem * pSocketItem = new CSocketItem();
    		//pSocketItem->Attach(m_wSocket, client.sin_addr.S_un.S_addr);
    
    
    		// Associate the newly arrived client socket with completion port
    		CreateIoCompletionPort((HANDLE)m_wSocket, CompletionPort, (DWORD)m_wSocket, 0);
    
    		// Launch an asynchronous operation for new arrived connection
    		pOverLapped = (p_COverLapped)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(COverLapped));
    		pOverLapped->Buffer.len = MSGSIZE;
    		pOverLapped->Buffer.buf = pOverLapped->szMessage;
    		pOverLapped->OperationType = RECV_POSTED;
    		WSARecv(m_wSocket, &pOverLapped->Buffer, 1, &pOverLapped->NumberOfBytesRecvd, &pOverLapped->Flags, &pOverLapped->overlap, NULL);
    	}
    
    	PostQueuedCompletionStatus(CompletionPort, 0xFFFFFFFF, 0, NULL);
    	CloseHandle(CompletionPort);
    	closesocket(m_wListen);
    	WSACleanup();
    
    	return 0;
    }
    
    DWORD WINAPI WorkerThread(LPVOID CompletionPortID)
    {
    	HANDLE                  CompletionPort = (HANDLE)CompletionPortID;
    	DWORD                   dwBytesTransferred;
    	SOCKET                  sClient;
    	p_COverLapped			pOverLapped = NULL;
    
    	while (TRUE)
    	{
    		GetQueuedCompletionStatus(CompletionPort, &dwBytesTransferred, (PULONG_PTR)&sClient, (LPOVERLAPPED *)&pOverLapped, INFINITE);
    		if (dwBytesTransferred == 0xFFFFFFFF)
    		{
    			return 0;
    		}
    
    		if (pOverLapped->OperationType == RECV_POSTED)
    		{
    			if (dwBytesTransferred == 0)
    			{
    				// Connection was closed by client
    				closesocket(sClient);
    				HeapFree(GetProcessHeap(), 0, pOverLapped);
    			}
    			else
    			{
    				pOverLapped->szMessage[dwBytesTransferred] = '';
    				send(sClient, pOverLapped->szMessage, dwBytesTransferred, 0);
    
    				// Launch another asynchronous operation for sClient
    				memset(pOverLapped, 0, sizeof(COverLapped));
    				pOverLapped->Buffer.len = MSGSIZE;
    				pOverLapped->Buffer.buf = pOverLapped->szMessage;
    				pOverLapped->OperationType = RECV_POSTED;
    				WSARecv(sClient, &pOverLapped->Buffer, 1, &pOverLapped->NumberOfBytesRecvd, &pOverLapped->Flags, &pOverLapped->overlap, NULL);
    			}
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    转:imageNamed和dataWithContentsOfFile的区别
    [内存管理实践 之 1]在返回按钮中,释放内存
    转:当程序崩溃的时候怎么办 Part2
    iOS 内存管理,我们需要一套切实可行的实践指导书,而不是理论指导书
    转 iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话)
    iOS笔记:内存管理
    转:【图文教程】创建Xcode自定义模板
    判断两个数的大小,返回其中的大者/小者
    iOS全局变量与属性的内存管理
    UIImage 详解
  • 原文地址:https://www.cnblogs.com/kingkoo/p/CompletionPort.html
Copyright © 2020-2023  润新知