The getsockname() socket call gets the local socket name. If you are using a SOCKS server, see Socket Secure Support for information about SOCKS.
Syntax
#include <types.h> #include <sys\socket.h> int getsockname(s, name, namelen) int s; struct sockaddr *name; int *namelen;
Parameters
s
Description
This call returns the name for the socket specified by the s parameter in the structure pointed to by the name parameter. It returns the address to the socket that has been bound. If the socket is not bound to an address, the call returns with the family set and the rest of the structure is set to zero. For example, an unbound socket in the internet domain would cause the name to point to a sockaddr_in structure with the sin_family field set to AF_INET and all other fields zeroed.
The namelen parameter must be initialized to indicate the size of the space pointed to by name and is set to the size of the local name copied. If the buffer is too small, the local name is truncated.
Sockets are explicitly assigned a name after a successful call to bind(). Stream and sequenced packet sockets are implicitly assigned a name after a successful call to connect() or accept() if bind() was not called.
If the socket is connected through a SOCKS server, this call returns the IP address and port of the local machine that is being used to communicate with the SOCKS server.
The getsockname() call is often used to discover the port assigned to a socket after the socket has been implicitly bound to a port. For example, an application can call connect() without previously calling bind(). In this case, the connect() call completes the binding necessary by assigning a port to the socket.
A process can use the getpeername() call to determine the address of a destination socket in a socket connection.
Return Values
The value 0 indicates success; the value -1 indicates an error. You can get the specific error code by calling sock_errno() or psock_errno().
Error Code
Related Calls