Posted on 2009-04-13 10:35
Prayer 閱讀(3091)
評論(0) 編輯 收藏 引用 所屬分類:
SOCKET
如果機器上有多塊網(wǎng)卡,兩個網(wǎng)卡處在不同的網(wǎng)段中,這在我們實驗室還是很常見的,兩個網(wǎng)卡有不同的默認網(wǎng)關(guān)。socket需要綁定相應(yīng)的網(wǎng)卡,將數(shù)據(jù)包發(fā)送到指定的網(wǎng)卡上。
上一段測試代碼:
#include <sys/socket.h>
#include <net/if.h>
struct ifreq interface;
struct socket sock;
/* Management net interface name */
#define IFNAME "eth1"
/* Acquire socket here ... */
strncpy(interface.ifr_ifrn.ifrn_name, IFNAME, \
IFNAMSIZ);
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, \
(char *)&interface, sizeof(interface)) < 0) {
perror("SO_BINDTODEVICE failed");
/* Deal with error... */
}
參考:
http://blog.chinaunix.net/u/270/showart_234383.html
http://tuxology.net/2008/05/15/forcing-connections-through-a-specific-interface/
文章出處:DIY部落(http://www.diybl.com/course/6_system/linux/Linuxjs/20090314/161536.html)