我的應用程序正在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