在完成端口中使用GetAcceptExSockaddrs


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.

09 09:56

你可能感兴趣的:(在完成端口中使用GetAcceptExSockaddrs)