Posted on 2009-04-13 10:35 
Prayer 阅读(3145) 
评论(0)  编辑 收藏 引用  所属分类: 
SOCKET 
			
			
		 
		如果机器上有多块网卡,两个网卡处在不同的网段中,这在我们实验室还是很常见的,两个网卡有不同的默认网关。socket需要绑定相应的网卡,将数据包发送到指定的网卡上。
上一段测试代码:
#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)