在完成連接埠中使用GetAcceptExSockaddrs

來源:互聯網
上載者:User

標籤:des   使用   io   strong   資料   for   art   ar   

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) );

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.