gtpc1m3s | Transmission Control Protocol/Internet Protocol |
The getpeername function returns the address of the peer
connected to socket s.
Format
#include <socket.h>
int getpeername(int s,
struct sockaddr *name,
int *namelen);
- s
- The socket descriptor.
- name
- Pointer to the sockaddr buffer. On return, the buffer
contains the name of the remote peer of the socket.
- namelen
- Size of the address structure pointed to by name, in
bytes. The namelen parameter 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. If the buffer
of the local host is too small, the peer name is truncated.
Normal Return
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.
- SOCNOTCONN
- The socket is not in the connected state.
- EIBMIUCVERR
- An error occurred while the function 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.
- SOCINVAL
- The namelen parameter is not a valid length. This error
code is returned only for TCP/IP native stack support.
- OFFLOADTIMEOUT
- The offload device did not issue a response to the function call in a
specified time period. This error code is returned only for TCP/IP
offload support.
Programming Considerations
- This function applies to all connected sockets for both TCP and UDP
protocols.
- When TCP/IP native stack support is defined, the length passed must be a
minimum of 32 bytes.
Examples
The following example obtains the peer socket address.
#include <socket.h>
·
·
·
int addrlen;
int rc,
int newclient_sock;
int server_sock;
struct sockaddr_in client_addr;
·
·
·
newclient_sock = accept(server_sock, (struct sockaddr *) 0, (int) 0);
·
·
·
addrlen = sizeof(client_addr);
rc = getpeername(newclient_sock, (struct sockaddr *)&client_addr,
&addrlen);
Related Information