【发布时间】:2011-04-29 06:05:10
【问题描述】:
我的应用程序在 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)?
【问题讨论】:
-
您为什么要这样做?除非您确定您的计算机将具有命名为预定义名称的接口,否则您的程序将失去可移植性。
-
用于嵌入式设备,不需要便携性。我有 6 个以太网端口,我需要使用特定接口发送数据
标签: c linux sockets network-programming raw-sockets