IOCP C ++ TCP客户端

时间:2011-04-19 14:14:36

标签: c++ windows tcp iocp

我在实施TCP IOCP客户端时遇到了一些麻烦。我已经在Mac OSX上实现了kqueue,所以我希望在Windows上做类似的事情,我的理解是IOCP是最接近的东西。主要问题是GetCompetetionStatus永远不会返回并且总是超时。我假设我在创建要监控的句柄时遗漏了一些东西,但不确定是什么。这是我到目前为止所处的位置:

我的连接例程:(为清晰起见,删除一些错误处理)

struct sockaddr_in server;
struct hostent *hp;
SOCKET sckfd;
WSADATA wsaData;

int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );


if ((hp = gethostbyname(host)) == NULL)
    return NULL;
WSASocket(AF_INET,SOCK_STREAM,0,NULL,0,WSA_FLAG_OVERLAPPED)
if ((sckfd = WSASocket(AF_INET,SOCK_STREAM,0, NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
{
    printf("Error at socket(): Socket\n");
    WSACleanup();
    return NULL;
}

server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr = *((struct in_addr *)hp->h_addr);
memset(&(server.sin_zero), 0, 8);

//non zero means non blocking. 0 is blocking.
u_long iMode = -1;
iResult = ioctlsocket(sckfd, FIONBIO, &iMode);
if (iResult != NO_ERROR)
    printf("ioctlsocket failed with error: %ld\n", iResult);


HANDLE hNewIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, ulKey, 0);
CreateIoCompletionPort((HANDLE)sckfd, hNewIOCP , ulKey, 0);

connect(sckfd, (struct sockaddr *)&server, sizeof(struct sockaddr));

//WSAConnect(sckfd, (struct sockaddr *)&server, sizeof(struct sockaddr),NULL,NULL,NULL,NULL);

return sckfd;   

以下是发送例程:(为了清晰起见,还删除了一些错误处理)

IOPortConnect(int ServerSocket,int timeout,string& data){

char buf[BUFSIZE];
strcpy(buf,data.c_str());
WSABUF buffer = { BUFSIZE,buf };
DWORD bytes_recvd;
int r;
ULONG_PTR ulKey = 0;
OVERLAPPED overlapped;
 OVERLAPPED* pov = NULL;
HANDLE port;

HANDLE hNewIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, ulKey, 0);
CreateIoCompletionPort((HANDLE)ServerSocket, hNewIOCP , ulKey, 0);


BOOL get = GetQueuedCompletionStatus(hNewIOCP,&bytes_recvd,&ulKey,&pov,timeout*1000);

if(!get)
    printf("waiton server failed. Error: %d\n",WSAGetLastError());
if(!pov)
    printf("waiton server failed. Error: %d\n",WSAGetLastError());

port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (u_long)0, 0);

SecureZeroMemory((PVOID) & overlapped, sizeof (WSAOVERLAPPED));

r = WSASend(ServerSocket, &buffer, 1, &bytes_recvd, NULL, &overlapped, NULL);
printf("WSA returned: %d WSALastError: %d\n",r,WSAGetLastError());
if(r != 0)
{
    printf("WSASend failed %d\n",GetLastError());
    printf("Bytes transfered: %d\n",bytes_recvd);
}
if (WSAGetLastError() == WSA_IO_PENDING)
    printf("we are async.\n");
CreateIoCompletionPort(port, &overlapped.hEvent,ulKey, 0);

BOOL test = GetQueuedCompletionStatus(port,&bytes_recvd,&ulKey,&pov,timeout*1000); 

CloseHandle(port);
return true;

}

任何见解都将受到赞赏。

2 个答案:

答案 0 :(得分:5)

您正在将同一套接字与多个IOCompletionPorts相关联。我相信这无效。在您的IOPortConnect函数中(写入的位置),您可以通过一次性句柄调用CreateIOCompletionPort 4次。

我的建议:

  • 创建一个IOCompletion端口(最终,您将多个套接字关联起来)。
  • 创建一个工作线程池(通过调用CreateThread),然后通过在循环中调用GetQueuedCompletionStatus来阻塞IOCompletionPort句柄。
  • 创建一个或多个WSA_OVERLAPPED套接字,并将每个套接字与IOCompletionPort相关联。
  • 使用带有OVERLAPPED *的WSA套接字函数来触发重叠操作。
  • 当工作线程从GetQueuedCompletionStatus返回并传入您传入的OVERLAPPED *以开始操作时,处理已发出请求的完成。

注意:WSASend返回0和SOCKET_ERROR,WSAGetLastError()为WSA_IO_PENDING作为代码,表示您将获得到达GetQueuedCompletionStatus的IO完成数据包。任何其他错误代码意味着您应该立即处理错误,因为IO操作没有排队,因此不会再有回调。

注2:传递给WSASend(或其他)函数的OVERLAPPED *是从GetQueuedCompletionStatus返回的OVERLAPPED *。您可以使用此事实通过调用传递更多上下文信息:

struct MYOVERLAPPED {
  OVERLAPPED ovl;
};
MYOVERLAPPED ctx;
WSASend(...,&ctx.ovl);
...
OVERLAPPED* pov;
if(GetQueuedCompletionStatus(...,&pov,...)){
  MYOVERLAPPED* pCtx = (MYOVERLAPPED*)pov;

答案 1 :(得分:2)

Chris处理了大部分问题,你可能已经看过很多示例代码了,但是......

我有一些可用的免费IOCP代码:http://www.serverframework.com/products---the-free-framework.html

还有一些关于该主题的CodeProject文章与该页面相关联。