Using a C++ TCP client socket on a specific network interface Linux/Unix

后端 未结 1 733
暗喜
暗喜 2021-01-07 01:46

I have the following code which by default connects to interface \"eth0\" which is a 1G NIC, but I would like to connect using \"eth5\", which is a 10G NIC.

         


        
相关标签:
1条回答
  • 2021-01-07 01:49

    According to the information here you can use setsockopt() to achieve this as follows:

    char* interface = "eth5";
    setsockopt( socket_file_descriptor_, SOL_SOCKET, SO_BINDTODEVICE, interface, 4 );
    

    The final parameter, 4, represents the number of bytes in the interface variable.

    0 讨论(0)
提交回复
热议问题