This function gets the local address for a socket.
At a Glance
Header file: | Winsock.h |
Windows CE versions: | 1.0 and later |
Syntax
int getsockname (SOCKET s, struct sockaddr *name, int *namelen);
Parameters
s
[in] Descriptor that identifies a bound socket.
name
[out] Receives the socket address (name).
namelen
[in/out] name buffer size.
Return Values
Zero indicates no error occurred. SOCKET_ERROR indicates failure. To get a specific error value, call WSAGetLastError.
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 set by the system.
On call, the namelen argument contains the name buffer size, in bytes. On return, the namelen parameter contains the actual name parameter size, in bytes.
The getsockname function does not always return data 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 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.
See Also