About the address_len parameter of accept and getpeername

Source: Internet
Author: User
Concerning the address_len parameter of accept and getpeername, in socket programming, it is inevitable that www.2cto needs to obtain peer information. comsocket provides two methods: accept () and getpeername () accept () intaccept (intsocket, s... concerning the address_len parameter of accept and getpeername, in socket programming, it is inevitable that when the peer information needs to be obtained, www.2cto.com socket itself provides us with two methods: accept () and getpeername () accept () int accept (int socket, struct sockaddr * restrict address, socklen_t * restrict address_len); socket: the socket here is the socke bound to the address T, can be understood as the socket on the server side. In the past, there was a listener (listen) operation. accept is its subsequent operation, and their operation objects are all the same socket. Www.2cto.com restrict address: address variable used to save the address information of the peer end. Address_len: the length of the restrict address parameter. when writing the restrict address information, the length of the write information is also saved to this variable. Note: this parameter is described in manpage and must be initialized before use. Returned value: if the request is successful, the opposite socket fd (non-negative) is returned. if the request fails,-1 is returned. (get errno at this time, errmsg is a good habit) getpeername () int getpeername (int sockfd, struct sockaddr * addr, socklen_t * addrlen); socket: the socket here is different from the socket in accept, and it should be a peer socket. Restrict address: same as address_len: the same as above. the address_len still involves a problem. I made this mistake: no initialization was made before use. Take a closer look at manpage. the fact about this parameter is as follows: take accept as an example. when accept receives a connection from the client, it will do two things: 1, write the peer information to the restrict address using the value of address_len. for example, if address_len is 1, write only one byte to the restrict address (or because the information length is XX, if the value is greater than 1, it is not written at all. this is more likely to be verified.) 2. actually, the length of the received peer information is written to address_len. so when your address_len variable is not initialized, the default value is 1 (Fedora-16), and the information cannot be fully written into the restrict address, resulting in the illusion that the peer information is not obtained.
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.