Beej's Guide Network to Programming series serialization 17

Source: Internet
Author: User

5.10. getpeername () --- who are you?
This function is too simple.

It's so simple that I don't even want to list a single chapter. But I did. The getpeername () function tells you who is on the other side of the connected streaming socket. The number of functions is as follows:

# Include <sys/socket. h>

Intgetpeername (int sockfd, struct sockaddr * addr, int * addrlen );

Sockfd is the descriptor of the connected stream socket. Addr is a pointer to the structure struct sockaddr (or structsockaddr_in), which stores information on the other side of the connection. Addrlen is an int pointer, Which is initialized to sizeof (struct sockaddr ).

When an error occurs, the function returns-1 and sets the corresponding errno.

Once you obtain their addresses, you can use inet_ntoa () or gethostbyaddr () to print or obtain more information. But you cannot get its account. (If it runs a silly daemon, this is possible, but it is beyond the scope of this article, please take a look at the RFC-1413 for more information .)

From the column xiaobin_HLJ80

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.