Name
getpeername - gets name of connected peer socket
Library
libc.lib
Synopsis
|
int
getpeername (int s, struct sockaddr * restrict name, socklen_t * restrict namelen);
|
Return values
The getpeername() function returns the value 0 if successful; otherwise
the value -1 is returned and the global variable errno is set to indicate
the error.
Detailed description
The
getpeername
system call
returns the name of the peer connected to
socket
s.
The
namelen
argument should be initialized to indicate
the amount of space pointed to by
name.
On return it contains the actual size of the name
returned (in bytes).
The name is truncated if the buffer provided is too small.
Examples
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
void GetSockName()
{
int sock_fd;
int newsock_fd;
struct sockaddr_in addr;
struct sockaddr_in ss;
struct sockaddr_in new_socket;
unsigned int len;
unsigned int addr_len;
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(5000);
bind(sock_fd,(struct sockaddr*)&addr,sizeof(addr));
listen(sock_fd,1);
newsock_fd = accept(sock_fd,(struct sockaddr*)&new_socket,&addr_len); // Code blocks here
// Assuming client has connected to the server.
len = sizeof(ss);
getpeername(sock_fd,(struct sockaddr*)&ss,&len);
close(newsock_fd);
close(sock_fd);
}
Errors
The call succeeds unless:
[EBADF]
|
|
The argument
s
is not a valid descriptor.
|
[ECONNRESET]
|
|
The connection has been reset by the peer.
|
[ENOTSOCK]
|
|
The argument
s
is a file, not a socket.
|
[ENOTCONN]
|
|
The socket is not connected.
|
[ENOBUFS]
|
|
Insufficient resources were available in the system
to perform the operation.
|
[EFAULT]
|
|
The
name
argument points to memory not in a valid part of the
process address space.
|
[EINVAL]
|
|
The
namelen
argument is invalid.
|
See also
accept,
bind,
getsockname,
socket
The
getpeername
system call appeared in
BSD 4.2.
Feedback
For additional information or queries on this page send feedback
© 2005-2007 Nokia
|
|