BOOL AcceptEx(
SOCKET sListenSocket,
SOCKET sAcceptSocket,
PVOID lpOutputBuffer,
DWORD dwReceiveDataLength,
DWORD dwLocalAddressLength,
DWORD dwRemoteAddressLength,
LPDWORD lpdwBytesReceived,
LPOVERLAPPED lpOverlapped
);
void GetAcceptExSockaddrs(
PVOID lpOutputBuffer,
DWORD dwReceiveDataLength,
DWORD dwLocalAddressLength,
DWORD dwRemoteAddressLength,
LPSOCKADDR* LocalSockaddr,
LPINT LocalSockaddrLength,
LPSOCKADDR* RemoteSockaddr,
LPINT RemoteSockaddrLength
);
如果想通過GetAcceptExSockaddrs 獲取本地地址和遠程地址,需要lpOutputBuffer指向當初調用AcceptEx時指定的一個緩衝區,並且在dwReceiveDataLength中指定相同的大小。
可以把這兩個操作看作是這樣的:GetAcceptEX獲取網絡數據,然後把地址信息按照指定的緩衝區位置填入緩衝區;GetAcceptExSockaddrs 則解析這個緩衝區,得到地址的指針。所以兩個buf和相關的長度必須一致,才能正確得到結果。
換句話說,經過AcceptEx操作之後,地址信息已經填入指定的緩衝區。我們可以通過指針操作得到那個地址,而不必使用GetAcceptExSockaddrs 。
MSDN的解釋:
The GetAcceptExSockaddrs function parses the data obtained from a call to the AcceptEx function and passes the local and remote addresses to a sockaddr structure.
Note This function is a Microsoft-specific extension to the Windows Sockets specification.
void GetAcceptExSockaddrs(
PVOID lpOutputBuffer,
DWORD dwReceiveDataLength,
DWORD dwLocalAddressLength,
DWORD dwRemoteAddressLength,
LPSOCKADDR* LocalSockaddr,
LPINT LocalSockaddrLength,
LPSOCKADDR* RemoteSockaddr,
LPINT RemoteSockaddrLength
);
Parameters
lpOutputBuffer
[in] Pointer to a buffer that receives the first block of data sent on a connection resulting from an AcceptEx call. Must be the same lpOutputBuffer parameter that was passed to the AcceptEx function.
dwReceiveDataLength
[in] Number of bytes in the buffer used for receiving the first data. This value must be equal to the dwReceiveDataLength parameter that was passed to the AcceptEx function.
dwLocalAddressLength
[in] Number of bytes reserved for the local address information. Must be equal to the dwLocalAddressLength parameter that was passed to the AcceptEx function.
dwRemoteAddressLength
[in] Number of bytes reserved for the remote address information. This value must be equal to the dwRemoteAddressLength parameter that was passed to the AcceptEx function.
LocalSockaddr
[out] Pointer to the sockaddr structure that receives the local address of the connection (the same information that would be returned by the getsockname function). This parameter must be specified.
LocalSockaddrLength
[out] Size of the local address, in bytes. This parameter must be specified.
RemoteSockaddr
[out] Pointer to the sockaddr structure that receives the remote address of the connection (the same information that would be returned by the getpeername function). This parameter must be specified.
RemoteSockaddrLength
[out] Size of the local address, in bytes. This parameter must be specified.