Understanding INADDR_ANY for socket programming
Solution 1:
bind()
ofINADDR_ANY
does NOT "generate a random IP". It binds the socket to all available interfaces.For a server, you typically want to bind to all interfaces - not just "localhost".
If you wish to bind your socket to localhost only, the syntax would be
my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1");
, then callbind(my_socket, (SOCKADDR *) &my_sockaddr, ...)
.-
As it happens,
INADDR_ANY
is a constant that happens to equal "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 ...
-
If you're not already familiar with it, I urge you to check out Beej's Guide to Sockets Programming:
http://beej.us/guide/bgnet/
Since people are still reading this, an additional note:
man (7) ip:
When a process wants to receive new incoming packets or connections, it should bind a socket to a local interface address using bind(2).
In this case, only one IP socket may be bound to any given local (address, port) pair. When INADDR_ANY is specified in the bind call, the socket will be bound to all local interfaces.
When listen(2) is called on an unbound socket, the socket is automatically bound to a random free port with the local address set to INADDR_ANY.
When connect(2) is called on an unbound socket, the socket is automatically bound to a random free port or to a usable shared port with the local address set to INADDR_ANY...
There are several special addresses: INADDR_LOOPBACK (127.0.0.1) always refers to the local host via the loopback device; INADDR_ANY (0.0.0.0) means any address for binding...
Also:
bind() — Bind a name to a socket:
If the (sin_addr.s_addr) field is set to the constant INADDR_ANY, as defined in netinet/in.h, the caller is requesting that the socket be bound to all network interfaces on the host. Subsequently, UDP packets and TCP connections from all interfaces (which match the bound name) are routed to the application. This becomes important when a server offers a service to multiple networks. By leaving the address unspecified, the server can accept all UDP packets and TCP connection requests made for its port, regardless of the network interface on which the requests arrived.
Solution 2:
INADDR_ANY
is used when you don't need to bind a socket to a specific IP. When you use this value as the address when calling bind()
, the socket accepts connections to all the IPs of the machine.
Solution 3:
To bind socket with localhost, before you invoke the bind function, sin_addr.s_addr field of the sockaddr_in structure should be set properly. The proper value can be obtained either by
my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1")
or by
my_sockaddress.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
Solution 4:
INADDR_ANY
instructs listening socket to bind to all available interfaces. It's the same as trying to bind to inet_addr("0.0.0.0")
.
For completeness I'll also mention that there is also IN6ADDR_ANY_INIT for IPv6 and it's the same as trying to bind to ::
address for IPv6 socket.
#include <netinet/in.h>
struct in6_addr addr = IN6ADDR_ANY_INIT;
Also, note that when you bind IPv6 socket to to IN6ADDR_ANY_INIT
your socket will bind to all IPv6 interfaces, and should be able to accept connections from IPv4 clients as well (though IPv6-mapped addresses).
Solution 5:
INADDR_ANY is a constant, that contain 0 in value . this will used only when you want connect from all active ports you don't care about ip-add . so if you want connect any particular ip you should mention like as my_sockaddress.sin_addr.s_addr = inet_addr("192.168.78.2")