Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

61. Sockets: Advanced Topics > Retrieving Socket Addresses

Retrieving Socket Addresses

The getsockname() and getpeername() system calls return, respectively, the local address to which a socket is bound and the address of the peer socket to which the local socket is connected.

#include <sys/socket.h>

int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Note

Both return 0 on success, or -1 on error

For both calls, sockfd is a file descriptor referring to a socket, and addr is a pointer to a suitably sized buffer that is used to return a structure containing the socket address. The size and type of this structure depend on the socket domain. The addrlen argument is a value-result argument. Before the call, it should be initialized to the length of the buffer pointed to by addr; on return, it contains the number of bytes actually written to this buffer.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint