Doubt about INADDR_ANY

1

I am working on a project in which I want to communicate a computer with a Raspberry Pi using the UDP communication protocol. My doubt comes at the time of declaring the IP address. I have read on some pages that this applies to all local interfaces. My question is about the amplitude of the local term, when working with two different computers, can I use the INADDR_ANY, or are they considered outside the local scope and I will have to declare the IP addresses?

    
asked by sebs 27.05.2018 в 02:59
source

1 answer

1

When you create a socket on the server side, you have to assign an IP and a port so that clients can specify where to connect or where to send the UDP datagram.

If you know the IP of your server, you can specify that IP as part of the parameters that you pass to bind() , but this will imply two things:

  • If your server had several network interfaces, each with its IP, specifying a specific IP will only receive data coming from that interface.
  • If your server changes the IP, you should also change the program, or it will stop working.
  • For both reasons, it is not usual to specify a specific IP in the code, and it is more convenient to use the constant INADDR_ANY , which is actually the special IP 0.0.0.0 that represents "whatever the IPs of all the interfaces of this computer network. "

    By using INADDR_ANY , you therefore get two things:

  • You will admit data that comes from any of the network interfaces that your server has.
  • Even if the server changes its IP, the program will still work without changes.
  • Most computers, unless they are routers, will have only one network card, but at least two network interfaces (you can have more, virtual). You can see what you have with the ifconfig command. At a minimum you will see two: one with the IP that has been assigned to the network card, and another with the special IP 127.0.0.1 , which represents the "local" interface of loopback (called in linux) lo ). This is a special network interface that does not receive requests from "outside" but only from inside the machine. It is very useful when the client and the server are on the same computer.

    The usual use of bind() is to assign one of these two IPs:

    • 0.0.0.0 ( INADDR_ANY ) to support network card connections, without having to know what IP is. You will also receive connections from the loopback interface (and all other network interfaces, physical or virtual, you might have).
    • 127.0.0.1 to allow connections only from the loopback interface, which will prevent anyone from accessing the server, if not the client is running on the machine itself. This may seem a bit useless, but it makes sense if you use SSH tunnels because in that case who receives the data is a local process that then connects to the server through the loopback .

    Keep in mind, however, that all this has to do with the IPs of the network interfaces that the server has. These IPs could be public IPs (with which you could connect to them from any computer in the world), or more likely local IPs (type 192.168.0.1 , etc.) that are only accessible to computers in the local network. Typically in this case there will be a router doing NAT, and that router will have a public IP. By properly configuring the router with port forwarding you can make the server can be accessed "from the outside", but that has nothing to do with bind() or with INADDR_ANY .

        
    answered by 27.05.2018 / 15:05
    source