与 INADDR_ANY 绑定
如果我将套接字绑定到 INADDR_ANY,我知道它将接受服务器上配置的任何 IP 上的传入连接。假设我在进行 bind() 调用时配置了 1 个 IP,然后配置了一个新的 IP。 bind() 是否也会接受向新配置的 IP 发起的连接,还是仅适用于调用 bind() 时存在的 IP?
If I bind a socket to INADDR_ANY I know that it will accept incoming connections on any of IPs configured on the server. Lets say I have 1 IP configured when I make the bind() call and then a new IP gets configured. Will be bind() accept connections initiated to the newly configured IP also or does it work only for the IPs that were existing when bind() was called ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Linux 上,当您绑定到
INADDR_ANY
时,套接字将保持绑定到0.0.0.0
,并且无论本地 IP 地址如何变化,都将接受与任何本地 IP 地址的连接。仅当建立 TCP 连接时,单个连接才会绑定到接收它的 IP 地址。任何地址仍可能收到其他连接。On Linux when you bind to
INADDR_ANY
then the socket stays bound to0.0.0.0
and will accept connection to any local IP address no matter how that changes. Only when a TCP connection is established then the single connection is bound to the IP address it was received on. Other connections may still be received on any address.是的,它将接受新创建或新配置的接口上的连接。
您可以通过创建一个虚拟接口来自己尝试:
或者其他什么;然后尝试连接到该IP。
Yes it will accept connections on newly created or newly configured interfaces.
You can try it yourself, by creating a dummy interface:
Or something; then try to connect to that IP.