UDT Reference: Functions

#### getsockname

The getsockname method retrieves the local address associated with a UDT socket.

int getsockname(
UDTSOCKET u,
struct sockaddr* name,
int* namelen
);

##### Parameters

u
  [in] Descriptor identifying a connected socket.
name
  [out] The structure to store the local address.
addrlen
  [in, out] pointer to the size of the name structure.

##### Return Value

On success, getlasterror returns 0 and the local address information is stored in name; otherwise it returns UDT::ERROR and the specific error information can be retrieved using getlasterror.

Error Name | Error Code | Comment
EINVPARAM | 5003 | Invalid parameters.
EINVSOCK | 5004 | u is an invailid UDT socket.
EUNBOUNDSOCK | 5005 | u is not bound to a local address yet.

##### Description

The getsockname retrieves the local address associated with the socket. The UDT socket must be bound explicitly (via bind) or implicitly (via connect), otherwise this method will fail because there is no meaningful address bound to the socket.

If getsockname is called after an explicit bind, but before connect, the IP address returned will be exactly the IP address that is used for bind and it may be 0.0.0.0 if ADDR_ANY is used. If getsockname is called after connect, the IP address returned will be the address that the peer socket sees. In the case when there is a proxy (e.g., NAT), the IP address returned will be the translated address by the proxy, but not a local address. If there is no proxy, the IP address returned will be a local address. In either case, the port number is local (i.e, not the translated proxy port).

Because UDP is connection-less, using getsockname on a UDP port will almost always return 0.0.0.0 as IP address (unless it is bound to an explicit IP) . As a connection oriented protocol, UDT will return a meaningful IP address by getsockname if there is no proxy translation exist.

UDT has no multihoming support yet. When there are multiple local addresses and more than one of them can be routed to the destination address, UDT may not behave properly due to the multi-path effect. In this case, the UDT socket must be explicitly bound to one of the local addresses.

##### See Also

listen, bind, connect