C语言 理解 INADDR_ANY 进行套接字编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16508685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding INADDR_ANY for socket programming
提问by epsilones
I am trying to program some sockets and so, on the server side, I use htonl(INADDR_ANY). To the extent I understood, it seems to me that this function generates a random IP (am I correct ?). In fact, I want to bind my socket with my localhost. But if I run this
我正在尝试编写一些套接字,因此,在服务器端,我使用htonl(INADDR_ANY). 在我理解的范围内,在我看来这个函数会生成一个随机 IP(我说得对吗?)。事实上,我想将我的套接字与我的localhost. 但是如果我运行这个
printf("%d",htonl(INADDR_ANY));
I get 0 as a return value. Could someone bring some explanation ?
我得到 0 作为返回值。有人可以带来一些解释吗?
回答by paulsm4
bind()ofINADDR_ANYdoes 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_ANYis 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:
bind()的INADDR_ANY的确不是“随机生成一个IP”。它将套接字绑定到所有可用的接口。对于服务器,您通常希望绑定到所有接口——而不仅仅是“本地主机”。
如果您只想将套接字绑定到本地主机,则语法为
my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1");,然后调用bind(my_socket, (SOCKADDR *) &my_sockaddr, ...).碰巧,
INADDR_ANY是一个恰好等于“零”的常数: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 ...如果您还不熟悉它,我强烈建议您查看 Beej 的套接字编程指南:
Since people are still reading this, an additional note:
由于人们仍在阅读本文,因此请补充说明:
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...
当一个进程想要接收新的传入数据包或连接时,它应该使用bind(2)将套接字绑定到本地接口地址。
在这种情况下,只有一个 IP 套接字可以绑定到任何给定的本地(地址、端口)对。当在绑定调用中指定 INADDR_ANY 时,套接字将绑定到所有本地接口。
当在未绑定的套接字上调用listen(2) 时,该套接字会自动绑定到一个随机的空闲端口,并将本地地址设置为 INADDR_ANY。
当在未绑定的套接字上调用connect(2) 时,该套接字将自动绑定到随机空闲端口或本地地址设置为 INADDR_ANY 的可用共享端口...
有几个特殊地址: INADDR_LOOPBACK (127.0.0.1) 总是通过环回设备引用本地主机;INADDR_ANY (0.0.0.0) 表示用于绑定的任何地址...
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.
如果 (sin_addr.s_addr) 字段设置为常量 INADDR_ANY,如 netinet/in.h 中所定义,则调用者请求将套接字绑定到主机上的所有网络接口。随后,来自所有接口(与绑定名称匹配)的 UDP 数据包和 TCP 连接被路由到应用程序。当服务器向多个网络提供服务时,这变得很重要。通过不指定地址,服务器可以接受为其端口发出的所有 UDP 数据包和 TCP 连接请求,而不管请求到达的网络接口。
回答by Barmar
INADDR_ANYis 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.
INADDR_ANY当您不需要将套接字绑定到特定 IP 时使用。当您使用此值作为调用时的地址时bind(),套接字将接受与本机所有 IP 的连接。
回答by MichaelGoren
To bindsocket with localhost, before you invoke the bindfunction, sin_addr.s_addr field of the sockaddr_in structure should be set properly. The proper value can be obtained either by
要将套接字与localhost绑定,在调用绑定函数之前,应正确设置 sockaddr_in 结构的 sin_addr.s_addr 字段。可以通过以下方式获得适当的值
my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1")
or by
或由
my_sockaddress.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
回答by Pavel P
INADDR_ANYinstructs 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_INITfor IPv6 and it's the same as trying to bind to ::address for IPv6 socket.
INADDR_ANY指示侦听套接字绑定到所有可用接口。这与尝试绑定到inet_addr("0.0.0.0"). 为了完整起见,我还将提到IPv6也有IN6ADDR_ANY_INIT,这与尝试绑定到::IPv6 套接字的地址相同。
#include <netinet/in.h>
struct in6_addr addr = IN6ADDR_ANY_INIT;
Also, note that when you bind IPv6 socket to to IN6ADDR_ANY_INITyour socket will bind to all IPv6 interfaces, and should be able to accept connections from IPv4 clients as well (though IPv6-mapped addresses).
另外,请注意,当您将 IPv6 套接字绑定到IN6ADDR_ANY_INIT您的套接字时,您的套接字将绑定到所有 IPv6 接口,并且也应该能够接受来自 IPv4 客户端的连接(尽管 IPv6 映射地址)。
回答by vivek
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")
INADDR_ANY 是一个常量,在 value 中包含 0。仅当您想从不关心 ip-add 的所有活动端口进行连接时才使用。所以如果你想连接任何特定的 ip 你应该提到像 my_sockaddress.sin_addr.s_addr = inet_addr("192.168.78.2")

