Socket bind-inaddr_any

Source: Internet
Author: User

When I first came into contact with bind, I only learned to bind the TCP-based server before Listen to ensure that the socket can listen on a fixed port. In bind mode, you only need to enter the port to be bound in the function parameter. For the IP address, you need to enter the IP address of the local machine, but you can also replace it with a macro inaddr_any, with this macro, you can replace the IP address of the Local Machine without looking for the IP address of the local machine. At that time, I only thought this inaddr_any was amazing, but I thought it was very convenient to use it, and I didn't go into any problems.
However, when I was working on the RTSP Server recently, I had to take a closer look at the BIND function.
We know that both UDP and TCP will correspond to a local IP address and port. We often call this IP address and port the socket source address and source port. When we use socket as a client to send data, we seldom consider what the source address and source port are. What we care about is its destination address and port. We usually only consider this source port when listening, so we will use bind when listening. After bind, the kernel will lock the source port of this socket to the port we set. But there is a problem. The BIND port is used to bind the socket with no source port to the port we specified, or will a socket with a port already allocated be redirected to the port we specified?
"If a tcp client or server has never called bind to bind a port, when you call connect or listen, the kernel selects a temporary interface for the corresponding socket." It can be determined from this sentence that, when the socket function is called to create a socket, the kernel has not allocated the source address and source port to the socket. For UDP, I guess that when sendto is called to send data, the kernel will randomly allocate ports without bundling ports.
The special application I encountered requires me to tell the other party my sending port before sending data using UDP, which means that I must bind the port before sendto, therefore, before sending data, I have to call the BIND function to bind the port. However, I am thinking that the kernel has the ability to randomly allocate ports, and all I need is to bind it to services that do not need to be bound to fixed ports, socket should be able to provide this service. Then I found that bind has this capability. When the port address in the BIND parameter is 0, the kernel allocates the port. In this way, I don't have to worry about repeated port addresses, but I can leave this problem to the kernel.
While discovering the bind mechanism, I found that bind also supports this method for source addresses. When the system has multiple IP addresses (multiple NICS, when we set the IP parameter in the BIND function to 0, the kernel allocates the IP address. Inaddr_any, which has always been amazing, is actually not magical at all. Its value is actually 0. So when we only have a single IP address, we can use inaddr_any to replace that single IP address, because we can only select this IP address when allocating the kernel. As a result, inaddr_any is the IP address of the local machine.

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.