多个接口绑定在同一个端口上 [英] multiple interface bind on same port
问题描述
假设一个网卡对于每个以太网有两个不同的 IP 地址,例如
suppose a nic having two different IP address for each ethernet for e.g
eth0 having IP address if 1.2.3.4
eth1 having IP address if 5.6.7.8
我可以为每个 IP 地址打开一个端口的连接,例如 1234
can i open a connection with one port e.g 1234 for each ip address for example
1.2.3.4 binds on 1234
and
5.6.7.8 binds on 1234
或者我应该得到端口已经被绑定的错误
or i should be getting the error the port is already be bind
我可以为 IPv4 执行此操作,为 IPv6 执行此操作,不确定同一台机器上的不同 IP 地址和相同端口是否有效
i can do this one for IPv4 and one for IPv6, not sure whether different ip address and same port on the same machine will work or not
推荐答案
一个网卡有两个不同的ip?什么是eth0,eth1?它显示了两个网卡.
A nic has two different ip? what is eth0, eth1? it shows two nics.
无论如何,如果您的机器有两个网卡,您可以绑定.但是你的机器有一个 nic 和两个 ip 地址,使用 INADDR_ANY.
anyway, if your machine has two nics, your can bind. but your machine has one nic and two ip addresses, use INADDR_ANY.
struct sockaddr_in sin;
sin.sin_family = AF_NET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(1234);
bind( sock, ...., (struct sockaddr*)&sin, ... );
如果接口使用 IPv6 地址,则套接字域必须为 AF_INET6.
if an interfaces use IPv6 Address, socket domain must be AF_INET6.
int sock = socket( AF_INET6 , ... ,... );
这篇关于多个接口绑定在同一个端口上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!