开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜