7 out of 8 rated this helpful - Rate this topic

getsockname function

Applies to: desktop apps only

The getsockname function retrieves the local name for a socket.

Syntax

int getsockname(
  __in     SOCKET s,
  __out    struct sockaddr *name,
  __inout  int *namelen
);

Parameters

s [in]

Descriptor identifying a socket.

name [out]

Pointer to a SOCKADDR structure that receives the address (name) of the socket.

namelen [in, out]

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 codeMeaning
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

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winsock2.h

Library

Ws2_32.lib

DLL

Ws2_32.dll

See also

Winsock Reference
Winsock Functions
bind
getpeername
SOCKADDR
socket

 

 

Send comments about this topic to Microsoft

Build date: 4/24/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
How to get the correct local IP when connecting with SSH?
When I try to get the local IP when connecting with SSH, I always get 127.0.0.1. I want to know how to get the real local IP. Below is my code. CString GetLocalIP() { CString strResult; char host[INET6_ADDRSTRLEN]; memset(host,0,INET6_ADDRSTRLEN); struct sockaddr_storage addr; socklen_t addr_len = sizeof(addr); if(getsockname(m_sock, (struct sockaddr*)&addr, &addr_len) == SOCKET_ERROR) { m_error = WSAGetLastError(); return false; } AddressToString((struct sockaddr *)&addr, addr_len, host); LPTSTR p = strResult.GetBuffer(INET6_ADDRSTRLEN); strcpy(p,host); strResult.ReleaseBuffer(); return strResult; }
ISA Firewall client changes behavior

http://blogs.msdn.com/ishai/archive/2004/06/08/151418.aspx
If you already have a connection with the other host, or with a host that is accessible to both, you can use getsockname() on that socket. If the client is behind an ISA Server and using ISA Firewall Client, using this method will retrieve the “external” IP address of the ISA server. This is good because you can later also bind() using this address which will cause the firewall client to send a bind request to the ISA server.

http://technet.microsoft.com/en-us/library/cc302546.aspx