• 在完成端口中使用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) );
  • 相关阅读:
    Bootstrap 栅格系统的总结
    你真的了解font-weight吗?
    常用jquery
    ios 遮罩层,切换后内容滑动到最上方,并且不可点击
    Canvas与Image互相转换示例以及实现微信长按自动识别二维码功能
    解决手机上页面返回但是页面js没有刷新的痛点
    rem定义
    二维码转换格式
    bootstrap框架的基本使用
    响应式开发及其原理
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3895220.html
Copyright © 2020-2023  润新知