如何在有 2 个网卡的情况下设置 UDP 多播套接字?

发布于 2024-07-10 18:32:10 字数 2783 浏览 9 评论 0原文

我正在尝试使用套接字和 C++ (c) 获取 udp 多播数据。 我有一台带有 2 个网卡的服务器,因此我需要将套接字绑定到特定接口。 目前我正在另一台只有一个网卡的服务器上进行测试。

当我使用 INADDR_ANY 时,我可以看到 udp 数据,当我绑定到特定接口时,我看不到任何数据。 函数 inet_addr 没有失败(我现在删除了对返回值的检查)。

代码如下。 在只有一张网卡的服务器上,我的IP地址是10.81.128.44。 当我运行时收到数据: ./client 225.0.0.37 12346

这没有给我任何数据: ./client 225.0.0.37 12346 10.81.128.44

有什么建议吗? (希望代码能够编译,我删除了注释...)

   #include <stdlib.h>
    #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 <iostream>
    #include <string>

    using namespace std;

    #define HELLO_PORT 12345
    #define HELLO_GROUP "225.0.0.37"
    #define MSGBUFSIZE 256

    int main(int argc, char *argv[])
    {
        string source_iface;
        string group(HELLO_GROUP);
        int port(HELLO_PORT);

        if (!(argc < 2)) group = argv[1];
        if (!(argc < 3)) port = atoi(argv[2]);
        if (!(argc < 4)) source_iface = argv[3];

        cout << "group: " << group << " port: " << port << " source_iface: " << source_iface << endl;

        int fd;
        if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
            perror("socket");
            exit(1);
        }

        u_int yes = 1;
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
        {
            perror("Reusing ADDR failed");
            exit(1);
        }

        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = (source_iface.empty() ? htonl(INADDR_ANY) : inet_addr(source_iface.c_str()));

   if (bind(fd,(struct sockaddr *)&addr, sizeof(addr)) < 0)
   {
        perror("bind");
        exit(1);
   }

   struct ip_mreq mreq;
   mreq.imr_multiaddr.s_addr = inet_addr(group.c_str());
   mreq.imr_interface.s_addr = (source_iface.empty() ? htonl(INADDR_ANY) : inet_addr(source_iface.c_str()));

    if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
    {
        perror("setsockopt");
        exit(1);
    }

    socklen_t addrlen;
    int nbytes;
    char msgbuf[MSGBUFSIZE];

   while (1)
    {
        memset(&msgbuf, 0, MSGBUFSIZE);

        addrlen = sizeof(addr);
        if ((nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *)&addr, &addrlen)) < 0)
        {
            perror("recvfrom");
            exit(1);
        }
       cout.write(msgbuf, nbytes);
        cout.flush();
    }

    return 0;
}

提前致谢...

I'm trying to get udp multicast data using sockets and c++ (c). I have a server with 2 network cards so I need to bind socket to specific interface. Currently I'm testing on another server that has only one network card.

When I use INADDR_ANY I can see the udp data, when I bind to specific interface I don't see any data. Function inet_addr is not failing (I removed checking for return value for now).

Code is below.
On a server with one network card, my IP address is 10.81.128.44. I receive data when I run as:
./client 225.0.0.37 12346

This gives me no data:
./client 225.0.0.37 12346 10.81.128.44

Any suggestions? (Hope the code compiles, I removed comments ...)

   #include <stdlib.h>
    #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 <iostream>
    #include <string>

    using namespace std;

    #define HELLO_PORT 12345
    #define HELLO_GROUP "225.0.0.37"
    #define MSGBUFSIZE 256

    int main(int argc, char *argv[])
    {
        string source_iface;
        string group(HELLO_GROUP);
        int port(HELLO_PORT);

        if (!(argc < 2)) group = argv[1];
        if (!(argc < 3)) port = atoi(argv[2]);
        if (!(argc < 4)) source_iface = argv[3];

        cout << "group: " << group << " port: " << port << " source_iface: " << source_iface << endl;

        int fd;
        if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
            perror("socket");
            exit(1);
        }

        u_int yes = 1;
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
        {
            perror("Reusing ADDR failed");
            exit(1);
        }

        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = (source_iface.empty() ? htonl(INADDR_ANY) : inet_addr(source_iface.c_str()));

   if (bind(fd,(struct sockaddr *)&addr, sizeof(addr)) < 0)
   {
        perror("bind");
        exit(1);
   }

   struct ip_mreq mreq;
   mreq.imr_multiaddr.s_addr = inet_addr(group.c_str());
   mreq.imr_interface.s_addr = (source_iface.empty() ? htonl(INADDR_ANY) : inet_addr(source_iface.c_str()));

    if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
    {
        perror("setsockopt");
        exit(1);
    }

    socklen_t addrlen;
    int nbytes;
    char msgbuf[MSGBUFSIZE];

   while (1)
    {
        memset(&msgbuf, 0, MSGBUFSIZE);

        addrlen = sizeof(addr);
        if ((nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *)&addr, &addrlen)) < 0)
        {
            perror("recvfrom");
            exit(1);
        }
       cout.write(msgbuf, nbytes);
        cout.flush();
    }

    return 0;
}

Thanks in advance ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2024-07-17 18:32:10

经过一番搜索和测试后,我发现此处绑定 udp 多播套接字我们指定端口并将地址保留为空,例如指定INADDR_ANY

所以下面的内容

addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = (source_iface.empty() ?
                        htonl(INADDR_ANY) : 
                        inet_addr(source_iface.c_str()));

应该是这样的:

评论:如果我理解你的代码,你
应该绑定到您的多播
地址不是通配符地址。 如果
您绑定到通配符地址
将能够接收单播
多播端口上的数据包。
绑定到您的多播地址将
防止这种情况发生并确保您只得到
该端口上的多播数据包。

编辑:根据上述注释修复了代码,绑定到多播地址,
存储在“组”中,而不是
INADDR_ANY 仅接收发送到的多播数据包
多播地址。

addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = (group.empty() ?
                        htonl(INADDR_ANY) :
                        inet_addr(group.c_str()));

这解决了问题。 添加IP_MULTICAST_IF不会有帮助,因为这是为了选择发送udp数据的特定接口,上面的问题是在接收端。

After some searching and testing I found out here that when binding udp multicast socket we specify port and leave address empty e.g. specify INADDR_ANY.

So the following

addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = (source_iface.empty() ?
                        htonl(INADDR_ANY) : 
                        inet_addr(source_iface.c_str()));

should be look like:

COMMENT: If I understand your code you
should be binding to your multicast
address not the wildcard address. If
you bind to the wildcard address you
will be able to receive unicast
packets on your multicast port.
Binding to your multicast address will
prevent this and ensure you only get
multicast packets on that port.

EDIT: Fixed the code based on above comment, binding to multicast address,
stored in 'group', as opposed to
INADDR_ANY to receive only multicast packets sent to
multicast address.

addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = (group.empty() ?
                        htonl(INADDR_ANY) :
                        inet_addr(group.c_str()));

This solved the problem. Adding IP_MULTICAST_IF will not help because that is for selecting specific interface for sending udp data, the problem above was on receiving side.

酒几许 2024-07-17 18:32:10

我认为您需要添加 IP_MULTICAST_IF

    struct  ip_mreq         multi;

   multi.imr_multiaddr.s_addr = inet_addr(group.c_str());
   multi.imr_interface.s_addr = (source_iface.empty() ?
         htonl(INADDR_ANY): inet_addr(source_iface.c_str()));

    status = setsockopt(me->ns_fd, IPPROTO_IP, IP_MULTICAST_IF,
        (char *)&multi.imr_interface.s_addr,
        sizeof(multi.imr_interface.s_addr));

我希望这有帮助。

I think you need to add IP_MULTICAST_IF

    struct  ip_mreq         multi;

   multi.imr_multiaddr.s_addr = inet_addr(group.c_str());
   multi.imr_interface.s_addr = (source_iface.empty() ?
         htonl(INADDR_ANY): inet_addr(source_iface.c_str()));

    status = setsockopt(me->ns_fd, IPPROTO_IP, IP_MULTICAST_IF,
        (char *)&multi.imr_interface.s_addr,
        sizeof(multi.imr_interface.s_addr));

I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文