繁体   English   中英

在 macOS Big Sur 上接收 UDP 多播

[英]Receiving UDP multicast on macOS Big Sur

我知道这可能是接收 UDP 多播消息的第 10.000 个问题。 但是,我已经在半年前开始工作了。 The following snippet I got from http://www.cs.tau.ac.il/~eddiea/samples/Multicast/multicast-listen.c.html (with minor changes: The bind-ip was changed to IPADDR_ANY ) worked as在我升级到 macOS Big Sur 之前就已经预料到了。

/**************************************************************/
/* Multicast listener (server)                                */
/*                                                            */
/* Activation using: {program name} {Multicast IP} {port}     */
/*   {program name} - This program name                       */
/*   {Multicast IP} - The IP address to listen to (Class D)   */
/*   {port} - The port hnumber to listen on                   */
/*                                                            */
/* This is free software released under the GPL license.      */
/* See the GNU GPL for details.                               */
/*                                                            */
/* (c) Juan-Mariano de Goyeneche. 1998, 1999.                 */
/**************************************************************/


#include <stdio.h>          /* printf(), snprintf() */
#include <stdlib.h>         /* strtol(), exit() */
#include <sys/types.h>
#include <sys/socket.h>     /* socket(), setsockopt(), bind(), recvfrom(), sendto() */
#include <errno.h>          /* perror() */
#include <netinet/in.h>     /* IPPROTO_IP, sockaddr_in, htons(), htonl() */
#include <arpa/inet.h>      /* inet_addr() */
#include <unistd.h>         /* fork(), sleep() */
#include <sys/utsname.h>    /* uname() */
#include <string.h>         /* memset() */

#define MAXLEN 1024


int main(int argc, char* argv[])
{
  u_char no = 0;
  u_int yes = 1;      /* Used with SO_REUSEADDR.
                             In Linux both u_int */
  /* and u_char are valid. */
  int send_s, recv_s;     /* Sockets for sending and receiving. */
  u_char ttl;
  struct sockaddr_in mcast_group;
  struct ip_mreq mreq;
  struct utsname name;
  int n;
  socklen_t socklen;
  struct sockaddr_in from;
  char message [MAXLEN+1];

  if (argc != 3) {
    fprintf(stderr, "Usage: %s mcast_group port\n", argv[0]);
    exit(1);
  }

  memset(&mcast_group, 0, sizeof(mcast_group));
  mcast_group.sin_family = AF_INET;
  mcast_group.sin_port = htons((unsigned short int)strtol(argv[2], NULL, 0));
  mcast_group.sin_addr.s_addr = htonl(INADDR_ANY);

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

  if (setsockopt(recv_s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
    perror("reuseaddr setsockopt");
    exit(1);
  }

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

  struct in_addr sin;
  inet_pton(AF_INET, argv[1], &sin);
  /* Preparatios for using Multicast */
  mreq.imr_multiaddr = sin;
  mreq.imr_interface.s_addr = 0;

  /* Tell the kernel we want to join that multicast group. */
  if (setsockopt(recv_s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
    perror ("add_membership setsockopt");
    exit(1);
  }

  for (;;) {
    socklen=sizeof(from);
    if ( (n=recvfrom(recv_s, message, MAXLEN, 0,
                     (struct sockaddr*)&from, &socklen)) < 0) {
      perror ("recv");
      exit(1);
    }
    message[n] = '\0'; /* null-terminate string */
    printf("%s: Received message from %s, size=%d !!\n",
           name.nodename,
           inet_ntoa(from.sin_addr), n);
    printf("\t%s \n", message);
  }
}

现在我尝试让它再次运行,但我找不到任何原因使它不再起作用。 Big Sur 的某些政策是否发生了变化,这是一些权限错误? 我已经使用sudo./multicast-listen 239.255.255.250 1900以 root 身份运行应用程序,但这也不起作用。 我正在尝试接收 SSDP 包。

所以@CraigEstey 的评论让我得出结论,它必须对防火墙做一些事情。 事实上,这是一个简单的防火墙问题。 Settings -> Security -> Firewall -> Firewall Options...中,您必须禁用Block all incoming connections并禁用隐身模式。 现在我按预期接收所有 SSDP 包。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM