我的应用程序正在CentOS 5.5上运行。 我使用原始套接字发送数据:如何将原始套接字绑定到特定接口
sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sd < 0) {
// Error
}
const int opt_on = 1;
rc = setsockopt(m_SocketDescriptor, IPPROTO_IP, IP_HDRINCL, &opt_on, sizeof(opt_on));
if (rc < 0) {
close(sd);
// Error
}
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = my_ip_address;
if (sendto(m_SocketDescriptor, DataBuffer, (size_t)TotalSize, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0) {
close(sd);
// Error
}
我如何可以绑定这个套接字到特定的网络接口(比如eth1的)?
你为什么要这么做?除非您确定您的计算机将具有名为预定义名称的接口,否则您的程序将失去可移植性。 – 2010-10-22 16:52:44
这是嵌入式设备,不需要携带便携性。 我有6个以太网端口,我需要使用特定的接口发送数据 – Dima 2010-10-22 17:47:24