无法从 linux 台计算机向多播组发送 UDP

Unable to send UDP to a multicast group from a linux computer

我正在尝试从 Linux 上的计算机 运行 向 IP 摄像机发送 UDP 数据报,以便发现它。所有设备都通过交换机连接。

有一个与此相机兼容的 windows 应用程序,它向多播组发送 UDP 数据报,并通过同一组从相机获得应答。我通过 Wireshark 发现了这一点,并决定尝试从我的 Linux 机器上的 C 程序发送相同的数据报。如果我的 C 程序将它直接发送到摄像机的 IP 地址,我能够得到正确的响应,但如果我将它发送到多播组则不能。

因此我在我的 Linux 电脑上制作了一个监听程序并试图将它发送到多播地址。这成功了,但我没有在 Wireshark 中捕获任何内容。

看来我的 C 程序能够正确发送数据报,只是没有发送到网络上。这可能是某种 Linux 配置吗?

编辑:根据要求,这是代码。仅供参考:截至目前,我只是尝试将数据发送到相机并检查 Wireshark 中的响应。

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

#include "udp_socket.h"



int main( void )
{

    //int s = udp_socket(50000);
    int s = socket(AF_INET,SOCK_DGRAM,0);
    printf("Socket: %d\r\n", s);


    char buffer[48] = {0x67,0x45,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x67,0x45,0x00,0x00,0x14,0x00,0x00,0x00,0x0a,0x00,0x00,0x00,0xea,0xc8,0xc8,0xc8,0xf4,0xe6,0x00,0x00};
    struct sockaddr_in serveraddr;
    memset( &serveraddr, 0, sizeof(serveraddr) );
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons( 59125 );              
    serveraddr.sin_addr.s_addr = inet_addr("234.200.200.200");

    udp_socket_tx(s, buffer, sizeof(buffer), &serveraddr );

    close(s);
}

udp_socket_tx() 来自 udp_socket.h:

int udp_socket_tx(int socket_fd, char * tx_buffer, int tx_buffer_len, const struct sockaddr_in * to_addr) {

return sendto(
    socket_fd,
    tx_buffer,
    tx_buffer_len,
    0,
    (const struct sockaddr *)to_addr,
    sizeof(struct sockaddr_in)
);

}

建议代码如下:

  1. 干净地编译。
  2. 执行所需的操作(发送 UDP 数据包)。
  3. 正确检查调用 sendto().
  4. 的返回状态
  5. 正确检查调用 socket().
  6. 的返回状态
  7. 不包含未使用的头文件。
  8. 期望多播地址在系统中 'route' table.
  9. 尊重打印页面的宽度
  10. 为调用 printf() 添加缺失的 #include
  11. 为调用 memset() 添加缺失的 #include
  12. 正确输出错误信息到stderr

现在建议的代码:

#include <stdio.h>      // perror(), printf()
#include <stdlib.h>     // exit(), EXIT_FAILURE, EXIT_SUCCESS
#include <string.h>     // memset()
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>


int main( void )
{
    int s = socket(AF_INET,SOCK_DGRAM,0);
    if ( 0 > s )
    {
        perror( "socket failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, socket successful

    printf("Socket: %d\r\n", s);

    unsigned char buffer[] =
    {
        0x67, 0x45,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x00,
        0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00, 0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x67,0x45,
        0x00, 0x00,0x14,0x00,0x00,0x00,0x0a,0x00,0x00,0x00,
        0xea, 0xc8, 0xc8, 0xc8, 0xf4, 0xe6, 0x00, 0x00
    };

    struct sockaddr_in serveraddr;
    memset( &serveraddr, 0, sizeof(serveraddr) );
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons( 59125 );
    serveraddr.sin_addr.s_addr = inet_addr("234.200.200.200");

    ssize_t sendtoStatus = sendto
    (
        s,
        buffer,
        sizeof(buffer),
        0,
        (const struct sockaddr *)&serveraddr,
        sizeof(struct sockaddr_in)
    );

    if( -1 == sendtoStatus )
    {
        perror( "sendto failed" );
        close(s);
        exit( EXIT_FAILURE );
    }

    close(s);
    return EXIT_SUCCESS;
} // end function: main

发送多播数据报时,它们通常会在被视为默认接口的网络接口上发送出去。

如果这不是您要发送的接口,您需要配置传出多播接口。这是通过 IP_MULTICAST_IF 选项完成的:

例如,如果你想从 IP 为 192.168.1.1 的接口发送多播,你可以按如下方式设置它:

struct in_addr multi_interface;
multi_interface.s_addr = inet_addr("192.168.1.1");
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
               (char *)&multi_interface, sizeof(multi_interface)) == -1) {
    perror("Error setting outgoing interface");
    close(s);
    exit(1);
}