m_SockFd = socket(AF_INET, SOCK_STREAM, 0);
if (m_SockFd==-1)
{
LOG4CPLUS_ERROR(logger, "opening stream socket ," <<__FILE__ <<" , "<<__LINE__ );
return 1;
}
// 绑定网络路由,eth0 or wlan0
struct ifreq struIR;
char sDev[16]={0};
if(GlobalConfig::IsHasGateWay("eth0") == true){
strcpy(sDev,"eth0");
LOG4CPLUS_WARN(logger, "tcpclient SO_BINDTODEVICE 1sDev = " << sDev);
}
else if(GlobalConfig::IsHasGateWay("wlan0") == true){
strcpy(sDev,"wlan0");
LOG4CPLUS_WARN(logger, "tcpclient SO_BINDTODEVICE 2sDev = " << sDev);
}
LOG4CPLUS_WARN(logger, "tcpclient SO_BINDTODEVICE sDev = " << sDev);
if(strstr(sDev,"eth0") || strstr(sDev,"wlan0")){
strncpy(struIR.ifr_name, sDev, IFNAMSIZ);
if (setsockopt(m_SockFd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&struIR, sizeof(struIR)) < 0){
perror("setsockopt SO_BINDTODEVICE error\n");
LOG4CPLUS_WARN(logger, "tcpclient SO_BINDTODEVICE error\n");
}
}
linux socket 设置从哪个网络设备发送数据 SO_BINDTODEVICE
最新推荐文章于 2024-04-10 15:30:34 发布