我有這個代碼可以向一個組發送多播訊息。運行程式時沒有錯誤,但是當我在 Wireshark 中監視資料包時,我的資料包的以太網目的地是我的默認網關,而不是類似的東西01-00-5e-xx-xx-xx
代碼:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
void main(int argc, char **argv){
int sockfd;
struct in_addr interface;
struct sockaddr_in group;
char readbuf[1024];
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0){
perror("socket error");
}
memset(&group,0,sizeof(group));
group.sin_family=AF_INET;
group.sin_addr.s_addr=inet_addr("244.244.244.1");
group.sin_port=htons(5555);
char loop=0;
if(setsockopt(sockfd,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop))<0){
perror("setsockopt error(IP_MULTICAST_LOOP)");
}
interface.s_addr=inet_addr("192.168.1.69");
if(setsockopt(sockfd,IPPROTO_IP,IP_MULTICAST_IF,&interface,sizeof(interface))<0){
perror("setsockopt error(IP_MULTICAST_IF)");
}
for(;;){
fgets(readbuf,sizeof(readbuf),stdin);
if(sendto(sockfd,readbuf,sizeof(readbuf),0,(struct sockaddr *)&group,sizeof(struct sockaddr))==-1){
perror("sendto error");
}
}
}
uj5u.com熱心網友回復:
244.244.244.1 不是有效的多播地址。
多播地址在 224.0.0.1 - 239.255.255.255 范圍內。您要發送的地址不在該范圍內。因此傳出 MAC 地址不是多播 MAC。
將目標 IP 更改為在多播 IP 地址范圍內,您將看到正確的多播 MAC 地址。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/441951.html
