开发者

How to send data by a given device (which is not a default device)? [duplicate]

开发者 https://www.devze.com 2023-04-03 10:09 出处:网络
This question already has an answer here: Closed 11 years ago. Possible Duplicate: Using a C++ TCP client socket on a specific network interf开发者_JAVA技巧ace Linux/Unix
This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

Using a C++ TCP client socket on a specific network interf开发者_JAVA技巧ace Linux/Unix

I want to write a socket program on Ubuntu, and there are many network devices in my PC. The program will send a UDP packet to ServerA by DeviceA, but in routing table, default device to the ServerA is DeviceB, how do I send the packet by DevideA without modify the routing table?

Thanks a lot!

/* I use C or C++ */

/* Note: DeviceA is a wireless interface, e.g: "wlan0". */

/* Note: I found a way on internet --> libnet, but it's not support the wireless interface. */


I believe you have to use the SO_BINDTODEVICE option with setsockopt. The code is something like this (untested):

const char *interface = "wlan0";

int sock = socket(AF_INET, SOCK_DGRAM, 0);
setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface));
0

精彩评论

暂无评论...
验证码 换一张
取 消