7.3. Processing responses
....
- Some name servers send their responses from different
addresses than the one used to receive the query. That is, a
resolver cannot rely that a response will come from the same
address which it sent the corresponding query to. This name
server bug is typically encountered in UNIX systems.
Parameters
s
A descriptor identifying a connected socket.
name
The SOCKADDR structure that receives the address of the peer.
namelen
A pointer to the size, in bytes, of the name parameter.
Return Value
If no error occurs, getpeername returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
Error code Meaning
WSANOTINITIALISED
A successful WSAStartup call must occur before using this function.
WSAENETDOWN
The network subsystem has failed.
WSAEFAULT
The name or the namelen parameter is not in a valid part of the user address space, or the namelen parameter is too small.
WSAEINPROGRESS
A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAENOTCONN
The socket is not connected.
WSAENOTSOCK
The descriptor is not a socket.
Remarks
The getpeername function retrieves the address of the peer connected to the socket s and stores the address in the SOCKADDR structure identified by the name parameter. This function works with any address family and it simply returns the address to which the socket is connected. The getpeername function can be used only on a connected socket.
For datagram sockets, only the address of a peer specified in a previous connect call will be returned. Any address specified by a previous sendto call will not be returned by getpeername.
On call, the namelen parameter contains the size, in bytes, of the name buffer. On return, the namelen parameter contains the actual size, in bytes, of the name parameter returned.
Requirements
Client
Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server
Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server.
166计计第二部分附Winsock API
下载
该函数用于获得通信方的套接字地址信息,该信息是关于已建立连接的那个套接字的。
它的定义如下:
第一个参数是准备连接的套接字,后两个参数则是指向基层协议类型及其长度的指针。
对数据报套接字来说,这个函数返回的是投向连接调用的那个地址;但不会返回投向 s e n d t o或
W S A S e n d To调用的那个地址。
2. getsockname
该函数是g e t s o c k n a m e的对应函数。它返回的是指定套接字的本地接口的地址信息。它的
定义如下:
除了套接字s返回的地址信息本地地址信息外,它的参数和g e t p e e r n a m e的参数都是一样的。
T C P协议中,这个地址和监听指定端口和I P接口的那个服务器套接字是一样的。
name
Pointer to a SOCKADDR structure that receives the address (name) of the socket.
namelen
Size of the name buffer, in bytes.
Return Value
If no error occurs, getsockname returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
Error code Meaning
WSANOTINITIALISED
A successful WSAStartup call must occur before using this API.
WSAENETDOWN
The network subsystem has failed.
WSAEFAULT
The name or the namelen parameter is not a valid part of the user address space, or the namelen parameter is too small.
WSAEINPROGRESS
A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAENOTSOCK
The descriptor is not a socket.
WSAEINVAL
The socket has not been bound to an address with bind, or ADDR_ANY is specified in bind but connection has not yet occurred.
Remarks
The getsockname function retrieves the current name for the specified socket descriptor in name. It is used on the bound or connected socket specified by the s parameter. The local association is returned. This call is especially useful when a connect call has been made without doing a bind first; the getsockname function provides the only way to determine the local association that has been set by the system.
On call, the namelen parameter contains the size of the name buffer, in bytes. On return, the namelen parameter contains the actual size in bytes of the name parameter.
The getsockname function does not always return information about the host address when the socket has been bound to an unspecified address, unless the socket has been connected with connect or accept (for example, using ADDR_ANY). A Windows Sockets application must not assume that the address will be specified unless the socket is connected. The address that will be used for the socket is unknown unless the socket is connected when used in a multihomed host. If the socket is using a connectionless protocol, the address may not be available until I/O occurs on the socket.
Requirements
Client
Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server
Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server.