zoukankan      html  css  js  c++  java
  • 在完成端口中使用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) );
  • 相关阅读:
    网络编程
    mysql
    python 基础
    vim 操作
    linux 基本命令
    基本库使用(urllib,requests)
    震撼功能:逐浪CMS全面支持PWA移动生成意指未来
    硬件能力与智能AI-Zoomla!逐浪CMS2 x3.9.2正式发布
    从送外卖到建站售主机还有共享自行车说起-2017年8月江西IDC排行榜与发展报告
    HTTP协议知多少-关于http1.x、http2、SPDY的相关知识
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3895220.html
Copyright © 2011-2022 走看看