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