简体   繁体   English

手动指定要发送数据的网络接口

[英]Manually specify which network interface to send data

I have implemented a service which sends and receives data over a tcp socket.我已经实现了一个通过 tcp 套接字发送和接收数据的服务。 Now there are two network interfaces on my machine, which I'm supposed to run the service over two networks.现在我的机器上有两个网络接口,我应该在两个网络上运行服务。

How can I specify which network interface to send data?如何指定要发送数据的网络接口? (Using Boost::asio or traditional Berkeley sockets, or by using a system call to change kernel's routing table on the fly) (使用 Boost::asio 或传统的 Berkeley 套接字,或使用系统调用来动态更改内核的路由表)

我不知道 Boost,但一般来说,如果您想使用特定接口发送/接收而不是将套接字bind()到该接口的 IP 地址,或者在某些平台上您可以使用setsockopt(SOL_SOCKET, SO_BINDTODEVICE)代替。

Did you tried this, create a socket bind to 0.0.0.0 for receiving packet from all interfaces.你试过这个吗,创建一个套接字绑定到 0.0.0.0 以接收来自所有接口的数据包。 Then create one socket for each interface for sending packet.然后为每个接口创建一个套接字用于发送数据包。 Key thing here is to set REUSEADDRSS option so that socket for sending can bind to address per-interface and sharing the same port of the one receiving packet.这里的关键是设置 REUSEADDRSS 选项,以便用于发送的套接字可以绑定到每个接口的地址并共享一个接收数据包的相同端口。

If you are dealing with TCP, an established connection will ready bind to a specific interface.如果您正在处理 TCP,已建立的连接将准备绑定到特定接口。 You won't be able to send to that connection by other interfaces.您将无法通过其他接口发送到该连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM