如何将套接字绑定到多个接口 [英] How to bind a socket to multiple interfaces
问题描述
在互联网上搜索了几个小时后,我仍然无法找到问题的答案。
After hours of searching on the internet, I still wasn't able to find an answer for my problem.
我的任务是创建一个接受套接字的服务器来自可变数量的接口(在配置文件中给出为 eth0,eth1等
)。
My task is to create a server that accepts sockets from a variable number of interfaces (given in a config file as eth0, eth1, etc.
).
什么这是最简单的方法吗?
有没有办法将一个套接字绑定到多个接口?我还没有办法做到这一点。
或者,我是否必须使用 INADDR_ANY
并以某种方式找出数据包所在的接口发送自?
What is the easiest way to do this?
Is there any way to bind one socket to multiple interfaces? I haven't found a way to do that yet.
Or, do I have to use INADDR_ANY
and somehow find out the interface which the packet was sent from?
有没有其他方法可以解决这个问题?
Is there another way to handle this problem?
推荐答案
您要么 bind (2)
一个套接字所有接口 INADDR_ANY
,或创建多个套接字并将每个绑定到所需接口的 IP地址。无论如何,请设置 套接字上的SO_REUSEADDR
选项。
You either bind(2)
one socket to all interfaces with INADDR_ANY
, or create multiple sockets and bind each to IP address of the desired interface. In any case, set SO_REUSEADDR
option on the sockets.
这篇关于如何将套接字绑定到多个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!