gtpc1m3v | Transmission Control Protocol/Internet Protocol |
The getsockname returns the name of the local socket.
Normal Return
#include <socket.h>
int getsockname(int s,
struct sockaddr *name,
int *namelen);
- s
- The socket descriptor.
- name
- Address of a sockaddr buffer into which getsockname
copies the local address of the socket.
- namelen
- Must initially point to an integer that contains the size, in bytes, of
the storage pointed to by name. On return, that integer
contains the size of the data returned in the storage pointed to by
name.
Format
Return code 0 indicates that the function was successful.
Error Return
A return code equal to -1 indicates an error. You can get the
specific error code by calling sock_errno. See Appendix C, Socket Error Return Codes for more information about socket errors.
- Note:
- Unless otherwise stated in the description, the following error codes can be
returned for either TCP/IP offload support or TCP/IP native stack
support.
- Value
- Description
- SOCFAULT
- Using name and namelen would result in an attempt to
access a protected address space. This error code is returned only for
TCP/IP offload support.
- SOCNOBUFS
- There is not enough buffer space. This error code is returned only
for TCP/IP offload support.
- SOCNOTSOCK
- The s parameter is not a valid socket descriptor.
- EIBMIUCVERR
- An error occurred while the function call was sent to the offload
device. This error code is returned only for TCP/IP offload
support.
- E1052STATE
- The socket was closed because the system was in or cycling down to 1052
state.
- EINACT
- All offload devices associated with the socket descriptor have been
disconnected. The socket is closed. This error code is returned
only for TCP/IP offload support.
- EINACTWS
- An offload device associated with the socket descriptor has been
disconnected. The socket is still available. This error code is
returned only for TCP/IP offload support.
- ESYSTEMERROR
- A system error has occurred and closed the socket. This error code
is returned only for TCP/IP offload support.
- OFFLOADTIMEOUT
- The offload device did not respond to the function call in a specified
time period. This error code is returned only for TCP/IP offload
support.
- SOCNOTCONN
- The socket is not bound to an IP address. This error code is
returned only for TCP/IP native stack support.
- SOCINVAL
- The namelen parameter is not a valid length. This error
code is returned only for TCP/IP native stack support.
Programming Considerations
- The getsockname function 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 the rest of the structure is set to 0.
- Stream sockets are not assigned a name until after a successful call to
the bind, connect, or accept function.
- The getsockname function is often used to find 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 function completes the binding necessary by assigning a
port to the socket. This assignment can be found with a call to
getsockname.
Examples
The following example obtains its socket address information.
#include <socket.h>
·
·
·
int addrlen;
int rc;
int server_sock;
struct sockaddr_in server_addr;
·
·
·
addrlen = sizeof(server_addr);
rc = getsockname(server_sock,(struct sockaddr *)&server_addr, &addrlen);
Related Information