Search code examples
socketsinterfacebind

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.

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.).

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?


Solution

  • 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.