Use getacceptexsockaddrs in the completed Port

Source: Internet
Author: User

Msdn: when usingAcceptex, 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, onceAcceptexFunction 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 usingGetsocknameFunction. Likewise, the remote address associated with the accepted socket can be retrieved usingGetpeernameFunction.

Unlike accept, acceptex is non-blocking and can initiate n acceptex before accepting client requests. When a connection is established (the three-way handshake ends ), there will be a Completion queue that accepts the completion package and adds it to the iocp. According to msdn, you can call getacceptexsockaddrs to parse (PARSE) 1) the first piece of data sent by the client in the work thread, 2) Local (server) socket address, 3) Remote (client) socket address

Worker thread:

// Obtain the customer address
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 );

As mentioned above, when the acceptex is complete and the so_update_accept_context option is set, you can also use getsockname to return the local address and getpeername to return the remote address.

The reason for setting so_update_accept_context is:

WhenAcceptexFunction returns, the socketSacceptsocketIs in the default state for a connected socket. The socketSacceptsocketDoes not inherit the properties of the socket associatedSlistensocketParameter until so_update_accept_context is set on the socket.

Settings:

UseSetsockoptFunction to set the so_update_accept_context option, specifyingSacceptsocketAs the socket handle andSlistensocketAs the option value.

For example:

 

 

 
Err = setsockopt (sacceptsocket, sol_socket, so_update_accept_context, (char *) & slistensocket, sizeof (slistensocket ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.