请教linux组播问题。
我的板子用嵌入式linux系统发送组播包给我PC虚拟机的linux,虚拟机的linux可以接收到,但是同样的程序,用虚拟机的linux发送,嵌入式linux却接收不到,具体表现为阻塞在recvfrom函数。
大致已经可以排除的问题:
1、网络、交换机等问题,2台设备之间直接用网线相连,不经过任何中间设备。
2、嵌入式linux内核问题,内核编译时我已经选上支持multicast,而且嵌入式linux能发出组播包说明内核起码在一定程度上肯定支持组播了,但不知道是否还有其他选项需要选?
以下是程序:
接收端程序
-----------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <memory.h>
#define PORT 9000
#define MultiAddr "224.0.1.1"
int main(void)
{
struct ip_mreq command;
int loop = 1;
int ttl=128;
int iter = 0;
int sin_len;
char message[256];
int socket_descriptor;
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
if((socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
loop = 1;
if(setsockopt(socket_descriptor,SOL_SOCKET, SO_REUSEADDR,&loop, sizeof(loop)) < 0)
{
perror("setsockopt:SO_REUSEADDR");
exit(EXIT_FAILURE);
}
if(bind(socket_descriptor,(struct sockaddr *)&sin, sizeof(sin)) < 0)
{
perror("bind");
exit(EXIT_FAILURE);
}
ttl=64;
if(setsockopt(socket_descriptor,IPPROTO_IP, IP_MULTICAST_TTL,&ttl, sizeof(ttl)) < 0)
{
perror("setsockopt:IP_MULTICAST_TTL");
exit(EXIT_FAILURE);
}
loop = 1;
if(setsockopt(socket_descriptor,IPPROTO_IP, IP_MULTICAST_LOOP,&loop, sizeof(loop)) < 0)
{
perror("setsockopt:IP_MULTICAST_LOOP");
exit(EXIT_FAILURE);
}
command.imr_multiaddr.s_addr = inet_addr(MultiAddr);
command.imr_interface.s_addr=inet_addr("192.168.0.97");
if(command.imr_multiaddr.s_addr == -1)
{
perror("224.0.0.1 not a legal multicast address");
exit(EXIT_FAILURE);
}
if (setsockopt(socket_descriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP,&command, sizeof(command)) < 0)
{
perror("setsockopt:IP_ADD_MEMBERSHIP");
}
while(iter++ < 8)
{
sin_len = sizeof(sin);
if(recvfrom(socket_descriptor,message,32,0,(struct sockaddr *)&sin, &sin_len) == -1)
{
perror("recvfrom");
}
printf("Response #%-2d from server: %s\n", iter, message);
sleep(2);
}
if(setsockopt(socket_descriptor, IPPROTO_IP, IP_DROP_MEMBERSHIP,&command, sizeof(command)) < 0)
{
perror("setsockopt:IP_DROP_MEMBERSHIP");
}
close(socket_descriptor);
exit(EXIT_SUCCESS);
}
------------
发送端程序
-------------------
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include &l