z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


getsockname()

z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
SC27-3660-00

The getsockname() call stores the current name for the socket specified by the s parameter into 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 sets the rest of the structure to 0. For example, an inbound 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 cleared.

Stream sockets are not assigned a name until a call is successful: bind(), connect(), or accept().

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. This assignment can be detected using a call to getsockname().

For AF_IUCV, namelen must be initialized to indicate the size of the space pointed to by name, and is set to the number of bytes copied into the space before the call returns. For AF_INET, the input value in the contents of namelen is ignored, but set before the call returns.

#include <manifest.h>
#include <bsdtypes.h>
#include <socket.h>
int getsockname(int s, struct sockaddr *name, int *namelen)
Parameter
Description
s
The socket descriptor.
name
The address of the buffer into which getsockname() copies the name of s.
namelen
Points to a fullword containing the size of the address structure pointed to by name in bytes.

Return values

A value of 0 indicates success; the value -1 indicates an error. Errno identifies the specific error.
Errno Value
Description
EBADF
The s parameter is not a valid socket descriptor.
EFAULT
Using the name and namelen parameters as specified would result in an attempt to access storage outside of the caller address space.

Related calls

accept(), bind(), connect(), getpeername(), socket()

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014