Understanding INADDR_ANY for socket programming

I am trying to program some sockets, so on the server side I use htonl(INADDR_ANY) . As I understand it, it seems to me that this function generates a random IP (am I correct?). In fact, I want to associate my socket with my localhost . But if I run this

 printf("%d",htonl(INADDR_ANY)); 

I get 0 as the return value. Can someone bring some kind of explanation?

+59
c sockets
May 12 '13 at 15:02
source share
5 answers
  1. bind () INADDR_ANY does NOT "generate a random IP". It associates a socket with all available interfaces .

  2. A server usually requires binding to all interfaces, and not just to the local host.

  3. If you want to bind your socket only to the local host, the syntax is my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1"); , then call bind(my_socket, (SOCKADDR *) &my_sockaddr,...) .

  4. As it happens, "INADDR_ANY" is a constant that turns out to be "zero":

    http://www.castaglia.org/proftpd/doc/devel-guide/src/include/inet.h.html

     # define INADDR_ANY ((unsigned long int) 0x00000000) ... # define INADDR_NONE 0xffffffff ... # define INPORT_ANY 0 ... 
  5. If you are not already familiar with this, I highly recommend that you check out the Beej Guide to Sockets Programming:

    http://beej.us/guide/bgnet/

As people are still reading this, an additional note:

male (7) ip :

When a process wants to receive new incoming packets or connections, it must bind the socket to the local interface address using bind (2) .

In this case, only one IP socket can be bound to any local one (address, port). When INADDR_ANY is specified in a bind call, the socket will be bound to all local interfaces.

When a call (2) is called on an unconnected socket, the socket is automatically bound to an arbitrary free port with the local address set to INADDR_ANY.

When the connect (2) call is made to an unconnected socket, the socket is automatically bound to an arbitrary free port or to a shared port with a local address set to INADDR_ANY ...

There are several special addresses: INADDR_LOOPBACK (127.0.0.1) always refers to the local host through a loopback device; INADDR_ANY (0.0.0.0) means any address to bind ...

Also:

bind () - bind a name to a socket :

If the (sin_addr.s_addr) field is set to the INADDR_ANY constant, as defined in netinet / in.h, the caller requests that the socket be bound to all network interfaces on the host. Subsequently, UDP packets and TCP connections from all interfaces (which correspond to the binding name) are sent to the application. This becomes important when a server offers a service for several networks. If you leave the address unspecified, the server can accept all UDP packets and TCP connection requests made for its port, regardless of the network interface over which the requests were received.

+80
May 12 '13 at 17:11
source share
โ€” -

INADDR_ANY used when you do not need to bind a socket to a specific IP address. When you use this value as the address when calling bind() , the socket accepts connections to all the IP addresses of the device.

+50
May 12, '13 at 15:17
source share

To associate a socket with localhost, before calling the bind function, you must set the value of the sin_addr.s_addr field of the sockaddr_in structure. The eigenvalue can be obtained either through

 my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1") 

or

 my_sockaddress.sin_addr.s_addr=htonl(INADDR_LOOPBACK); 
+6
Aug 15 '13 at 10:35 on
source share

INADDR_ANY is a constant containing the value 0. It will be used only when you want to connect from all active ports, you are not interested in ip-add. therefore, if you want to connect any particular ip, you should mention as my_sockaddress.sin_addr.s_addr = inet_addr ("192.168.78.2")

0
Jul 12 '17 at 7:53 on
source share

INADDR_ANY instructs the listening socket to bind to all available interfaces. It is similar to trying to bind to inet_addr("0.0.0.0") . For completeness, I also mentioned that there is also IN6ADDR_ANY_INIT for IPv6, and it is similar to trying to bind to :: address for an IPv6 socket.

 #include <netinet/in.h> struct in6_addr addr = IN6ADDR_ANY_INIT; 

Also note that with IN6ADDR_ANY_INIT socket IN6ADDR_ANY_INIT to IN6ADDR_ANY_INIT your socket will bind to all IPv6 interfaces and must also accept connections from IPv4 clients (albeit with IPv6-mapped addresses).

0
May 11 '18 at 5:52
source share



All Articles