我的 Winsock Delphi 应用程序应该在所有网络接口(interface)上监听多播 UDP/IP 流。它可以正常收听,直到我在另一台具有不同网络适配器优先级顺序的 PC 上尝试它。
然后我开始研究问题,在一些论坛上发现INADDR_ANY
(或0.0.0.0
)在Windows和Linux中有不同的含义:
- 在 Linux 中 it means "listen on all interfaces"和发送 - 通过默认接口(interface)发送
- 在 Windows 中 it means "listen on default interface" (
0.0.0.1
第二个)。引用:“如果此成员指定 0.0.0.0 的 IPv4 地址,则使用默认的 IPv4 多播接口(interface)”- 没有提及它是用于监听还是用于发送。
你能证实还是否认这一点?
如何在所有接口(interface)上真正监听?
这是我的一小段代码:
TMulticastListener = class(TThread)
private
mreq: ip_mreq;
............
end;
constructor TMulticastListener.Create;
var err: Integer;
wData: WsaData;
reuse: Integer;
begin
inherited Create(true);
err := WSAStartup(MAKEWORD(2, 2), wData);
if err = SOCKET_ERROR then begin
// Tell the user that we could not find a usable Winsock DLL
perror('WSAStartup');
Exit;
end;
// create what looks like an ordinary UDP socket
fd := socket(AF_INET, SOCK_DGRAM, 0);
if fd = INVALID_SOCKET then begin
perror('socket');
Exit;
end;
reuse := 1;
// allow multiple sockets to use the same PORT number
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, Pointer(@reuse), SizeOf(reuse)) < 0) then begin
perror('Reusing ADDR failed');
Exit;
end;
// set up destination address
FillChar(addr, sizeof(addr), 0);
addr.sin_family := AF_INET;
addr.sin_addr.s_addr := htonl(INADDR_ANY); // N.B.: differs from sender
addr.sin_port := htons(HELLO_PORT);
// bind to receive address
if (bind(fd, addr, sizeof(addr)) < 0) then begin
perror('bind');
Exit;
end;
// use setsockopt() to request that the kernel join a multicast group
mreq.imr_multiaddr.s_addr := inet_addr(HELLO_GROUP);
mreq.imr_interface.s_addr := htonl(INADDR_ANY); //inet_addr('0.0.0.0');
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, @mreq, sizeof(mreq)) < 0) then begin
perror('setsockopt');
Exit;
end;
end;
最佳答案
Windows 和 Linux 在 INADDR_ANY
的使用方面实际上表现相同。此处的混淆是因为您提供的两个链接用于不同的上下文。
当使用bind
函数绑定(bind)到地址/端口时,指定INADDR_ANY
意味着套接字将能够在给定端口上从任何接口(interface)接收数据包。但是,这样做不会设置有关多播的任何内容。
在 IP_ADD_MEMBERSHIP
调用 setsockopt
的上下文中,将接口(interface)设置为 INADDR_ANY
将使系统加入给定的多播组默认网络接口(interface)。
您提供的 Linux 链接是指 bind
,而 Windows 链接是指 setsockopt
和 IP_ADD_MEMBERSHIP
。
如果要在所有接口(interface)上加入多播组,则需要检索系统上的接口(interface)列表并加入每个接口(interface)。在 Windows 上,GetAdaptersAddresses()
函数将为您提供接口(interface)列表。
在 Linux 上,使用 getifaddrs()
功能。
这是一个如何在 C 语言中使用 GetAdaptersAddresses()
函数的例子:
struct iflist {
char name[50];
struct sockaddr_in sin;
int isloopback;
int ismulti;
int ifidx;
};
void getiflist(struct iflist *list, int *len)
{
IP_ADAPTER_ADDRESSES *head, *curr;
IP_ADAPTER_UNICAST_ADDRESS *uni;
char *buf;
int buflen, err, i;
buflen = 100000;
buf = calloc(buflen, 1);
head = (IP_ADAPTER_ADDRESSES *)buf;
if ((err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, head,
&buflen)) != ERROR_SUCCESS) {
char errbuf[300];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
0, errbuf, sizeof(errbuf), NULL);
printf("GetAdaptersAddresses failed: (%d) %s", err, errbuf);
free(buf);
return;
}
for (*len = 0, curr = head; curr; curr = curr->Next) {
if (curr->IfType == IF_TYPE_TUNNEL) continue;
for (uni = curr->FirstUnicastAddress; uni; uni = uni->Next) {
if (curr->OperStatus == IfOperStatusUp) {
memset(&list[*len], 0, sizeof(struct iflist));
strncpy(list[*len].name, (char *)curr->AdapterName,
sizeof(list[i].name) - 1);
memcpy(&list[*len].sin, uni->Address.lpSockaddr,
uni->Address.iSockaddrLength);
list[*len].isloopback =
(curr->IfType == IF_TYPE_SOFTWARE_LOOPBACK);
list[*len].ismulti =
((curr->Flags & IP_ADAPTER_NO_MULTICAST) == 0);
if (uni->Address.lpSockaddr->sa_family == AF_INET6) {
list[*len].ifidx = curr->Ipv6IfIndex;
} else {
list[*len].ifidx = curr->IfIndex;
}
(*len)++;
}
}
}
free(buf);
}
关于linux - Linux 和 Windows socket 编程中 INADDR_ANY 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41694289/