首页 > 代码库 > 在完成端口中使用GetAcceptExSockaddrs

在完成端口中使用GetAcceptExSockaddrs

MSDN:When using AcceptEx, the GetAcceptExSockaddrs function must be called to parse the buffer into its three distinct parts (data, local socket address, and remote socket address). On Windows XP and later, once the AcceptEx function completes and the SO_UPDATE_ACCEPT_CONTEXT option is set on the accepted socket, the local address associated with the accepted socket can also be retrieved using the getsockname function. Likewise, the remote address associated with the accepted socket can be retrieved using the getpeername function.

和accept不一样的是,AcceptEx是非阻塞的,而且在接受客户端请求前,可以发起n个AcceptEx,当连接建立时(三次握手结束时 刻),会有一个接受完成包加入IOCP的完成队列,按照MSDN上说,就可以在工作线程中,通过调用GetAcceptExSockaddrs解析 (parse)1)客户端发送的第一块数据,2)本地(Server)套接字地址,3)远程(Client)套接字地址

工作线程:

      // 取得客户地址
      int nLocalLen, nRmoteLen;
      LPSOCKADDR pLocalAddr, pRemoteAddr;
      m_lpfnGetAcceptExSockaddrs(
       pBuffer->buff,
       pBuffer->nLen - ((sizeof(sockaddr_in) + 16) * 2),
       sizeof(sockaddr_in) + 16,
       sizeof(sockaddr_in) + 16,
       (SOCKADDR **)&pLocalAddr,
       &nLocalLen,
       (SOCKADDR **)&pRemoteAddr,
       &nRmoteLen);

正如上面说当AcceptEx完成时,并且SO_UPDATE_ACCEPT_CONTEXT选项被设置时,也可以通过getsockname返回local address ,通过调用getpeername返回remote address

设置SO_UPDATE_ACCEPT_CONTEXT原因是:

When the AcceptEx function returns, the socket sAcceptSocket is in the default state for a connected socket. The socket sAcceptSocket does not inherit the properties of the socket associated with sListenSocket parameter until SO_UPDATE_ACCEPT_CONTEXT is set on the socket.

设置:

Use the setsockopt function to set the SO_UPDATE_ACCEPT_CONTEXT option, specifying sAcceptSocket as the socket handle and sListenSocket as the option value.

For example:

 

 

err = setsockopt( sAcceptSocket,     SOL_SOCKET,     SO_UPDATE_ACCEPT_CONTEXT,     (char *)&sListenSocket,     sizeof(sListenSocket) );