我的Winsock Delphi应用程序应该在所有网络接口上侦听多播UDP / IP流.它正常收听,直到我在具有不同网络适配器优先级顺序的另一台PC上尝试它.
然后我开始研究问题,并在一些论坛上发现INADDR_ANY(或0.0.0.0)在Windows和Linux中有不同的含义:
>在Linux it means “listen on all interfaces”中和发送 – 通过默认接口发送
>在Windows it means “listen on default interface”中(第二个为0.0.0.1).引用:“如果此成员指定IPv4地址为0.0.0.0,则使用默认的IPv4多播接口” – 不提及是用于侦听还是用于发送.
你能否证实或否认这一点?
如何在所有界面上真实收听?
这是我的一小段代码:
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;
最佳答案
在使用INADDR_ANY时,Windows和Linux实际上表现相同.这里的混淆是因为您提供的两个链接正在不同的上下文中使用.
原文链接:/linux/440682.html使用bind函数绑定到地址/端口时,指定INADDR_ANY意味着套接字将能够从任何接口接收给定端口上的数据包.但是,这样做不会设置有关多播的任何内容.
在对setsockopt的IP_ADD_MEMBERSHIP调用的上下文中,将接口设置为INADDR_ANY将使系统在默认网络接口上加入给定的多播组.
您提供的Linux链接指的是bind,而Windows链接指的是setsockopt和IP_ADD_MEMBERSHIP.
如果要在所有接口上加入组播组,则需要检索系统上的接口列表并加入每个接口.在Windows上,GetAdaptersAddresses()
功能将为您提供接口列表.
在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,NULL,head,&buflen)) != ERROR_SUCCESS) {
char errbuf[300];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,errbuf,sizeof(errbuf),NULL);
printf("GetAdaptersAddresses Failed: (%d) %s",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],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);
}