连接或数据包在哪个本地接口上接收?
- 当在绑定到
0.0.0.0
/INADDR_ANY
的未连接 UDP 套接字上接收数据包时,如何确定它发送到的本地 IP? - 我可以确定它是通过哪个接口接收的吗?
- 对于面向连接的套接字(例如 TCP)也可以这样做吗?
Update0
- 平台是 Linux,因此语言无关紧要,但 C 是原生的。
- UDP 套接字绑定到
INADDR_ANY
主机,因此getsockname()
返回0.0.0.0
。
- When receiving a packet on an unconnected UDP socket bound to
0.0.0.0
/INADDR_ANY
, how can I determine what the local IP it was sent to? - Can I determine what interface it was received on?
- Can this be also be done for connection-oriented sockets such as TCP?
Update0
- Platform is Linux, so language is irrelevant but C is native.
- UDP sockets are bound to
INADDR_ANY
host, sogetsockname()
returns0.0.0.0
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,看看这个。所以看起来可能有一个套接字选项,至少在 Linux/Unix 世界中是这样。它需要在哪些操作系统上运行?
Hmmm, have a look at this. So looks like there is probably a socket option, at least in the Linux/Unix world. What OSes does it need to work on?
我不得不在 Windows 平台上处理同样的问题。我的解决方案是显式侦听所有可用接口,这样
getsockname()
按预期工作。I've had to deal with the same issue on Windows platforms. My solution was to explicitly listen on all available interfaces as that way
getsockname()
works as expected.