TCP/IP Programming Manual
address_len_ptr
input and return value; maintained only for compatibility and should be a value indicating the
size in bytes of the structure (the remote address and port number) pointed to by address_ptr.
tag
input value; the tag parameter to be used for the nowait operation initiated by
getsockname_nw.
Errors
If an error occurs, the external variable errno is set to the following value:
An invalid argument was specified.EINVAL
Usage Guidelines
• Use getsockname on a socket created for waited operations, or use getsockname_nw on
a socket created for nowait operations. The operation initiated by getsockname_nw must
be completed with a call to the AWAITIOX procedure.
• This function does not return an address when called on an unconnected UDP socket. In
addition, this function does not return a port number for an unconnected UDP socket until the
first I/O operation on the socket is completed. This is typical of socket implementations.
• Declare the address_ptr variable as struct sockaddr_in6 * for IPv6 use or as
struct sockaddr_storage * for protocol-independent use. In C, when you make the
call, cast the variable to sockaddr. (See the IPv6 example.)
See Chapter 3 (page 62) for information about struct sockaddr *.
See Nowait Call Errors (page 86) for information on error checking.
Examples
INET: the following programming example gets the address and port number to which the socket
chan is bound:
#include <socket.h>
#include <in.h>
#include <netdb.h>
struct sockaddr_in lcl;
optlen = sizeof(lcl);
if (getsockname(chan,(struct sockaddr *)&lcl, &optlen) < 0)
perror ("Get socket name failed.");
/* Code to use the address and port number. */
INET6: the following programming example gets the address and port number to which the socket
chan is bound:
#include <socket.h>
#include <in.h>
#include <in6.h>
#include <netdb.h>
struct sockaddr_in6 lcl;
optlen = sizeof(lcl);
/* Notice that the lcl below is cast as sockaddr * as suggested
in the Usage Guidelines */
if (getsockname(chan,(struct sockaddr *)&lcl, &optlen) < 0)
perror ("Get socket name failed.");
/* Code to use the address and port number. */
getsockname, getsockname_nw 127