getsockname function (winsock2.h)
The getsockname function retrieves the local name for a socket.
Syntax
int WSAAPI getsockname(
[in] SOCKET s,
[out] sockaddr *name,
[in, out] int *namelen
);
Parameters
[in] s
Descriptor identifying a socket.
[out] name
Pointer to a SOCKADDR structure that receives the address (name) of the socket.
[in, out] 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 |
---|---|
A successful WSAStartup call must occur before using this API. | |
The network subsystem has failed. | |
The name or the namelen parameter is not a valid part of the user address space, or the namelen parameter is too small. | |
A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. | |
The descriptor is not a socket. | |
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.
Windows Phone 8: This function is supported for Windows Phone Store apps on Windows Phone 8 and later.
Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows Server 2012 R2, and later.
Requirements
Requirement | Value |
---|---|
Minimum supported client | Windows 8.1, Windows Vista [desktop apps | UWP apps] |
Minimum supported server | Windows Server 2003 [desktop apps | UWP apps] |
Target Platform | Windows |
Header | winsock2.h (include Winsock2.h) |
Library | Ws2_32.lib |
DLL | Ws2_32.dll |