Establishing a TCP connection using a specific NIC/IP address

In a scenario where a machine has multiple IP addreses and/or NICs, there is no way to connect using a specific one when using TCP.

TcpListener only has a method for accepting but not connecting, but adding for example a function named "connect" which consumes the Listener and returns a Socket would allow to keep the previous behavior and for better control of which local IP address is used. (at least this is how it works on Unix and Windows)

UdpSocket already has this functionality, as you have to bind it first anyway.

That's usually done by binding before connecting. Now libstd doesn't expose that for TcpStreams, but the net2 crate does: TcpBuilder::bind. There's an open issue on rust-lang/rust to maybe eventually bring that to libstd.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.