Socket INADDR_ANY listens to the 0.0.0.0 address. The socket only binds the port so that the route table decides which ip address to transfer.

Source: Internet
Author: User

 

Http://blogold.chinaunix.net/u1/43206/showart_518836.html

Socket INADDR_ANY listens to the 0.0.0.0 address. The socket only binds the port so that the route table decides which ip address to transfer.

INADDR_ANY indicates the address 0.0.0.0. In fact, this address indicates an uncertain address, "all addresses", or "any address ".

Generally, if you want to create a network server, you need to notify the server operating system at a specific address xxx. xxx. xxx. listen on a port of yyyy on xxx, and send the packet to me. This process is completed by calling the bind () system. -- That is, your program needs to bind a server address, or use a port on a server address as used. The server operating system can give you the specified address or not.

If your server has multiple NICs,
For some reason, your service (whether listening on a udp port or a tcp port) may increase or decrease the IP address of your server operating system at any time, it may also be to save the trouble of determining the network port (NIC) on the server-you can tell the operating system when calling bind: "I need to listen on the yyyy port, so the port sent to the server, no matter which NIC/IP address receives the data, is processed by me." At this time, the server listens on the address 0.0.0.0. For example:

Proto Recv-Q Send-Q Local Address Foreign Address (state)
......
Udp4 0 0 *. 7913 *.*
Udp4 0 0 *. 7911 *.*
Tcp4 0 0 *. ftp *. * LISTEN
......
......

These are network listening conditions. The Local Address is "*. ftp" and "*. 7911", which indicates that the service program is bound to all the network adapters of the server.

 

 

Specifically:

 

 

 

For example, your machine has three ip addresses.
192.168.1.1
202.202.202.202
61.1.2.3

If you serv. sin_addr.s_addr = inet_addr ("192.168.1.1 ");

Then listen to port 100

At this time, only connect 192.168.1.1: 100 on other machines can be successful.
Connect 202.202.202.202: 100 and connect 61.1.2.3: 100 both fail.

If serv. sin_addr.s_addr = htonl (INADDR_ANY);, the connected ip address can be connected.

 

 

Well, you understand what INADDR_ANY means. So, will my server receive repeated packets if there are n ip addresses? After receiving the packet, will the client reply again?
The answer is: no repeated data packets are received or sent repeatedly.

Why? Right upstairs, every server maintains a route table. The route table determines where data packets should be sent.

Why can't I receive repeated data packets?
A: The client only sends data to the unique IP address on your server.

Why not send data packets repeatedly?
A: The route (PATH) for sending data packets is unique. If the server does not know which address to send data when sending data, the data will be sent to the "Default Gateway.

How do I select the data transmission path?
A: send the message as required by the route table.

If there are duplicate/conflict records in the route table, how can we select the path at this time?
A: route table records are prioritized. Generally, if the route table record of a Windows operating system is repeated, the record to be added later prevails. In some operating systems, such as linux/FreeBSD, duplicate route table records cannot be added.

For a dedicated router, there is a routing algorithm. Generally, there are many routes that reach a certain point on the network. The routing algorithm can determine the "best path". The path either has the lowest latency, the lowest communication cost, or the highest bandwidth, it is either the smallest hop point-how to choose it depends on how the router administrator configures it.

If the client is bound to INADDR_ANY, the situation is similar. For TCP, it is bound to a specific IP address during the connect () System Call. The selection is based on the reachable IP address from the subnet where the address is located to the target address. At this time, the getsockname () System Call will show you which address to use. For UDP, the situation is special. Even if you use the connect () System Call, It is not bound to a specific address. This is because using connect () for UDP does not actually send any data that establishes a connection to the target address, nor verify the accessibility to the target address. It only records the information of the target address in the internal socket data structure and will be used later. Only when sendto ()/send () is called, The system kernel determines which address (NIC) sends UDP according to the route table.
Packet.

P.S.

-----------------------------------------------------------------------------

There is a route table in the IP layer:
In the MSDOS window, run the command netstat-r.
To display the route table. Sends data from the specified NIC based on the route table entries.
ARP cache: arp-
.
Generally, the destination MAC address of an Ethernet frame is the next hop.

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.