Hello World UDP组播无法正常工作

时间:2016-05-13 16:11:42

标签: c++ linux udp multicast

我试图让一个非常基本的hello world UDP发送器和UDP多播侦听器工作。我有一台PC,但有一台带有Linux OS CentOS的虚拟机。连接到互联网没有问题。发送者和监听者是两个独立的程序,Eclipse是我的环境。 发件人......

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>


#define UDP_PORT 5403
#define UDP_GROUP "225.0.0.1" // 127.0.0.1

int main(int argc, char *argv[])
{
     struct sockaddr_in addr;
     int fd;
     struct ip_mreq mreq;
     char *message="Hello, World!";
     int message_size = strlen(message) + 1;

     // Create a UDP socket
     fd = socket(AF_INET, SOCK_DGRAM, 0);
     if (fd < 0)
     {
         perror("socket(...) ");
         return -1;
     }

     // allow multiple sockets to use the same PORT number
     u_int reuse_port = 1;
     if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &reuse_port, sizeof(reuse_port)) < 0)
     {
         perror("setsockopt(...) ");
         return -1;
     }

     // set up destination address
     memset(&addr, 0, sizeof(addr));
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = inet_addr(UDP_GROUP);
     addr.sin_port = htons(UDP_PORT);

     printf("Begin sendto(...) infinite loop\n");
     while (true)
     {
         printf("Sending message: %s, of size: %d\n", message, message_size);
         if (sendto(fd, message, message_size, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
         {
             perror("sendto(...): ");
             return -1;
         }
         // printf("message sent: %s\n", message);
         sleep(1);
     }
     return 1;
}

听众......

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>

#define UDP_PORT 5403
#define UDP_GROUP "225.0.0.1"
#define MAX_BUFFER_SIZE 256

int main(int argc, char *argv[])
{
     struct sockaddr_in addr;
     int fd, nbytes;
     socklen_t addrlen;
     struct ip_mreq mreq;
     char msgbuf[MAX_BUFFER_SIZE];
     u_int reuse_port = 1;

     // Create a socket
     fd = socket(AF_INET,SOCK_DGRAM,0);
     if (fd < 0)
     {
        perror("create socket failed");
        return -1;
     }

     // allow multiple sockets to use the same PORT number
     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_port, sizeof(reuse_port)) < 0)
     {
        perror("Reusing port number failed");
        return -1;
     }

     // set up destination address
     memset(&addr, 0, sizeof(addr));
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = htonl(INADDR_ANY);
     addr.sin_port = htons(UDP_PORT);
     if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
     {
        perror("bind");
        return -1;
     }

     // Set the recvfrom timeout after 1 s
     struct timeval tv;
     tv.tv_sec = 2;
     tv.tv_usec = 0;
     if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
     {
         perror("Error setting recvfrom timeout\n");
         return -1;
     }

     // use setsockopt() to request that the kernel join a multicast group
     mreq.imr_multiaddr.s_addr = inet_addr(UDP_GROUP);
     mreq.imr_interface.s_addr = htonl(INADDR_ANY);
     if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
     {
        perror("setsockopt");
        return -1;
     }

     addrlen = sizeof(addr);
     printf("Begin recvfrom(...) infinite loop\n");
     while (true)
     {
         nbytes = recvfrom(fd, msgbuf, MAX_BUFFER_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
         if (nbytes < 0)
         {
            printf("recvfrom timeout\n");
         }
         else
         {
            printf("message received: %s\n", msgbuf);
         }
     }
     return 1;
}

每一秒,发送方程序printf&#34;发送消息:Hello,World!,大小:14&#34;并且每两秒钟接收器打印&#34;从超时&#34;中恢复。我已经设置了Wireshark来查看UDP流量,我肯定会看到sento数据。 recvfrom没有获得任何数据。我尝试过从255.0.0.0到239.255.255.255使用许多不同的Group IP,没有变化。我尝试了很多不同的端口,没有变化。他们需要在我的网卡上进行特殊设置吗?我不确定还能做什么。小编辑,recvfrom和sendto消息不应该有&#34;&amp;&#34;。

0 个答案:

没有答案