The use of Inaddr_any in Linux network programming

Source: Internet
Author: User

 network programming commonly used to bind function, you need to bind the IP address, you can set Inaddr_any  Inaddr_any is the designated address 0.0. 0 address, which in fact represents an indeterminate address, or "all addresses", "arbitrary addresses". That is to say that all the IP, because some of the machine more than one network card, multi-card case, this is the meaning of all network card IP address.  For example a computer has  3 network cards, respectively connected to three networks, then this computer has  3 IP addresses, If an application needs to listen on a port, which port does he want to listen to? If binding a specific IP address, you can only listen to the IP address you set up the network card of the port, the other two network card can not listen to the port, if I need three network cards are listening, then need to bind  3 IP, also equals the need to manage  3 sockets for data exchange, so it is not very cumbersome?  so you just have to bind Inaddr_any, manage a socket on the line, regardless of the data is from which Nic come over, as long as the port number is bound to come over the data, can be received. connect" inaddr_any. You must indicate which server IP you want to connect to. As shown in the code: 

servaddr.sin_addr.s_addr = htonl (inaddr_any);//indicates that the server listens to native all IP addresses




The use of Inaddr_any in Linux network programming

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.