perform connect() on specific network adapter

2020-07-29 02:32发布

问题:

I'm programming with Winsock2. I have two network adapters in system, one for local connections (LAN), one for outer connections (PPPOE). When I perform a connect() call to connect to local address, it uses PPPOE adapter instead of LAN. I know I could tweak this using metrics, but can't I just use some hard-code to forcibly use LAN adapter? Thanks in advance!

回答1:

Before you connect(), you need to bind() to the IP address of the LAN adapter. Just specify the IP address, and leave the port open (i.e. 0), so that the system can still chose one.