本文轉載來自CSDN博客
http://blog.csdn.net/nicholasmaxwell/archive/2006/05/18/744467.aspxhttp://blog.csdn.net/nicholasmaxwell/archive/2006/05/18/744464.aspx
另外一篇值得參考的《完成端口I/O模型編寫心得!》http://blog.csdn.net/jasonm2008/archive/2009/08/14/4441514.aspx#
#include "stdafx.h"
#include <iostream.h>
#include
#include
#include
#define PORT 5150
#define DATA_BUFSIZE 8192
typedef struct
{
OVERLAPPED OVerlapped;
WSABUF DATABuf;
CHAR Buffer[DATA_BUFSIZE];
DWORD BytesSend,BytesRecv;
}PER_IO_OPERATION_DATA, *LPPER_IO_OPERATION_DATA;
typedef struct
{
SOCKET Socket;
}PER_HANDLE_DATA,*LPPER_HANDLE_DATA;
DWORD WINAPI ServerWorkerThread(LPVOID ComlpetionPortID);
int main(int argc, char* argv[])
{
SOCKADDR_IN InternetAddr;
SOCKET Listen,Accept;
HANDLE CompetionPort;
SYSTEM_INFO SystenInfo;
LPPER_HANDLE_DATA PerHandleData;
LPPER_IO_OPERATION_DATA PerIOData;
int i;
DWORD RecvBytes;
DWORD Flags;
DWORD ThreadID;
WSADATA wsadata;
DWORD Ret;
if (Ret = WSAStartup(0x2020,&wsadata) != 0)
{
printf("WSAStartup failed with error %d\n",Ret);
return 0;
}
//打開一個空的完成端口
if ((CompetionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,0,0)) == NULL)
{
printf("CreateIoCompletionPort failed with error %d\n",GetLastError());
return 0;
}
GetSystemInfo(&SystenInfo);
// 開啟cpu個數的2倍個的線程
for (i=0; i < SystenInfo.dwNumberOfProcessors*2; i++)
{
HANDLE ThreadHandle;
//創建服務器工作線程,并且向線程傳送完成端口
if ((ThreadHandle = CreateThread(NULL,0,ServerWorkerThread,CompetionPort,0,&ThreadID)) == NULL)
{
printf("CreateThread failed with error %d\n" ,GetLastError());
return 0;
}
CloseHandle(ThreadHandle);
}
//打開一個服務器socket
if ((Listen = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
{
printf("WSASocket() failed with error %d\n", WSAGetLastError());
return 0;
}
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);
if (bind(Listen,(LPSOCKADDR)&InternetAddr,sizeof(InternetAddr)) == SOCKET_ERROR)
{
printf("bind failed with error %d\n",WSAGetLastError());
return 0;
}
if (listen(Listen,5) == SOCKET_ERROR)
{
printf("listen failed with error %d\n",WSAGetLastError());
return 0;
}
//接收連接并且分發給完成端口
while (TRUE)
{
if ((Accept = WSAAccept(Listen,NULL,NULL,NULL,0)) == SOCKET_ERROR)
{
printf("WSAAccept failed with error %d\n",WSAGetLastError());
return 0;
}
//創建與套接字相關的套接字信息結構
if ((PerHandleData = (LPPER_HANDLE_DATA)GlobalAlloc(GPTR,sizeof(PER_HANDLE_DATA))) == NULL)
{
printf("GlobalAlloc failed with error %d\n",GetLastError());
return 0;
}
// Associate the accepted socket with the original completion port.
printf("Socket number %d connected\n",Accept);
PerHandleData->Socket = Accept;//結構中存入接收的套接字
//與我們的創建的那個完成端口關聯起來,將關鍵項也與指定的一個完成端口關聯
if ((CreateIoCompletionPort((HANDLE)Accept,CompetionPort,(DWORD)PerHandleData,0)) == NULL)
{
printf("CreateIoCompletionPort failed with error%d\n",GetLastError());
return 0;
}
// 創建同下面的WSARecv調用相關的IO套接字信息結構體
if ((PerIOData = (LPPER_IO_OPERATION_DATA)GlobalAlloc(GPTR,sizeof(PER_IO_OPERATION_DATA))) = NULL)
{
printf("GlobalAloc failed with error %d\n",GetLastError());
return 0;
}
ZeroMemory(&(PerIOData->OVerlapped),sizeof(OVERLAPPED));
PerIOData->BytesRecv = 0;
PerIOData->BytesSend = 0;
PerIOData->DATABuf.len = DATA_BUFSIZE;
PerIOData->DATABuf.buf = PerIOData->Buffer;
Flags = 0;
if (WSARecv(Accept,&(PerIOData->DATABuf),1,&RecvBytes,&Flags,&(PerIOData->OVerlapped),NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSARecv() failed with error %d\n",WSAGetLastError());
return 0;
}
}
}
return 0;
}
工作者線程
//工作線程
DWORD WINAPI ServerWorkerThread(LPVOID ComlpetionPortID)
{
HANDLE ComplectionPort = (HANDLE) ComlpetionPortID;
DWORD BytesTransferred;
LPOVERLAPPED Overlapped;
LPPER_HANDLE_DATA PerHandleData;
LPPER_IO_OPERATION_DATA PerIOData;
DWORD SendBytes,RecvBytes;
DWORD Flags;
while (TRUE)
{
if (GetQueuedCompletionStatus(ComplectionPort,&BytesTransferred,(LPDWORD)&PerHandleData,(LPOVERLAPPED*)&PerIOData,INFINITE) == 0)
{
printf("GetQueuedCompletionStatus failed with error%d\n",GetLastError());
return 0;
}
//首先檢查套接字上是否發生錯誤,如果發生了則關閉套接字并且清除同套節字相關的SOCKET_INFORATION 結構體
if (BytesTransferred == 0)
{
printf("Closing Socket %d\n",PerHandleData->Socket);
if (closesocket(PerHandleData->Socket) == SOCKET_ERROR)
{
printf("closesocket failed with error %d\n",WSAGetLastError());
return 0;
}
GlobalFree(PerHandleData);
GlobalFree(PerIOData);
continue;
}
//檢查BytesRecv域是否等于0,如果是,說明WSARecv調用剛剛完成,可以用從己完成的WSARecv調用返回的BytesTransferred值更新BytesRecv域
if (PerIOData->BytesRecv == 0)
{
PerIOData->BytesRecv = BytesTransferred;
PerIOData->BytesSend = 0;
}
else
{
PerIOData->BytesRecv +=BytesTransferred;
}
//
if (PerIOData->BytesRecv > PerIOData->BytesSend)
{
//發布另一個WSASend()請求,因為WSASendi 不能確保發送了請的所有字節,繼續WSASend調用直至發送完所有收到的字節
ZeroMemory(&(PerIOData->OVerlapped),sizeof(OVERLAPPED));
PerIOData->DATABuf.buf = PerIOData->Buffer + PerIOData->BytesSend;
PerIOData->DATABuf.len = PerIOData->BytesRecv - PerIOData->BytesSend;
if (WSASend(PerHandleData->Socket,&(PerIOData->DATABuf),1,&SendBytes,0,&(PerIOData->OVerlapped),NULL) ==SOCKET_ERROR )
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSASend() fialed with error %d\n",WSAGetLastError());
return 0;
}
}
}
else
{
PerIOData->BytesRecv = 0;
//Now that is no more bytes to send post another WSARecv() request
//現在己經發送完成
Flags = 0;
ZeroMemory(&(PerIOData->OVerlapped),sizeof(OVERLAPPED));
PerIOData->DATABuf.buf = PerIOData->Buffer;
PerIOData->DATABuf.len = DATA_BUFSIZE;
if (WSARecv(PerHandleData->Socket,&(PerIOData->DATABuf),1,&RecvBytes,&Flags,&(PerIOData->OVerlapped),NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSARecv() failed with error %d\n",WSAGetLastError());
return 0;
}
}
}
}
}