联网计算机未接收多播

发布于 2024-11-27 15:49:55 字数 3231 浏览 4 评论 0原文

我正在尝试向所有网络计算机发送多播。我在我的计算机上设置了服务器,并在网络上的另一台计算机上设置了服务器。当我发送多播消息时,我计算机上运行的服务器可以很好地接收它。但联网的计算机却什么也得不到。我已经尝试将 TTL 设置为最大值,但没有起到任何作用。我也尝试过使用 WireShark 监控我的数据包,但没有看到任何东西(我对此不太熟练)。我很困惑为什么我的计算机收到它,但另一台联网的计算机却没有收到它。这是我用来发送多播的代码:

#include <sys/types.h>   /* for type definitions */
#include <winsock2.h>    /* for win socket API calls */
#include <ws2tcpip.h>    /* for win socket structs */
#include <stdio.h>       /* for printf() */
#include <stdlib.h>      /* for atoi() */
#include <string.h>      /* for strlen() */

#define MAX_LEN  1024    /* maximum string size to send */
#define MIN_PORT 1024    /* minimum port allowed */
#define MAX_PORT 65535   /* maximum port allowed */

int main(int argc, char *argv[]) {

  int sock;                   /* socket descriptor */
  char send_str[MAX_LEN];     /* string to send */
  struct sockaddr_in mc_addr; /* socket address structure */
  int send_len;               /* length of string to send */
  char* mc_addr_str;          /* multicast IP address */
  unsigned short mc_port;     /* multicast port */
  unsigned char mc_ttl=255;     /* time to live (hop count) */
  WSADATA wsaData;            /* Windows socket DLL structure */

  /* validate number of arguments */
  if (argc != 3) {
    fprintf(stderr, 
            "Usage: %s <Multicast IP> <Multicast Port>\n", 
            argv[0]);
    exit(1);
  }

  mc_addr_str = argv[1];       /* arg 1: multicast IP address */
  mc_port     = atoi(argv[2]); /* arg 2: multicast port number */

  /* validate the port range */
  if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
    fprintf(stderr, "Invalid port number argument %d.\n",
            mc_port);
    fprintf(stderr, "Valid range is between %d and %d.\n",
            MIN_PORT, MAX_PORT);
    exit(1);
  }

  /* Load Winsock 2.0 DLL */
  if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
    fprintf(stderr, "WSAStartup() failed");
    exit(1);
  }

  /* create a socket for sending to the multicast address */
  if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket() failed");
    exit(1);
  }

  /* set the TTL (time to live/hop count) for the send */
  if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, 
       (void*) &mc_ttl, sizeof(mc_ttl))) < 0) {
    perror("setsockopt() failed");
    exit(1);
  } 

  /* construct a multicast address structure */
  memset(&mc_addr, 0, sizeof(mc_addr));
  mc_addr.sin_family      = AF_INET;
  mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
  mc_addr.sin_port        = htons(mc_port);

  printf("Begin typing (return to send, ctrl-C to quit):\n");

  /* clear send buffer */
  memset(send_str, 0, sizeof(send_str));

  while (fgets(send_str, MAX_LEN, stdin)) {
    send_len = strlen(send_str);

    /* send string to multicast address */
    if ((sendto(sock, send_str, send_len, 0, 
         (struct sockaddr *) &mc_addr, 
         sizeof(mc_addr))) != send_len) {
      perror("sendto() sent incorrect number of bytes");
      exit(1);
    }

    /* clear send buffer */
    memset(send_str, 0, sizeof(send_str));
  }

  closesocket(sock);  
  WSACleanup();  /* Cleanup Winsock */

  exit(0);
}

I'm trying to send a multicast to all network computers. I have my server set up on my computer and another computer on the network. When I send out the multicast message, the server running on my computer picks it up fine. The networked computer doesn't get anything though. I've tried setting the TTL to its max value and that hasn't done anything. I've also tried monitoring my packets using WireShark, but didn't see anything (I'm not very skilled with this). I'm confused why my computer receives it, but not the other networked computer. Here is the code I'm using to send the multicast:

#include <sys/types.h>   /* for type definitions */
#include <winsock2.h>    /* for win socket API calls */
#include <ws2tcpip.h>    /* for win socket structs */
#include <stdio.h>       /* for printf() */
#include <stdlib.h>      /* for atoi() */
#include <string.h>      /* for strlen() */

#define MAX_LEN  1024    /* maximum string size to send */
#define MIN_PORT 1024    /* minimum port allowed */
#define MAX_PORT 65535   /* maximum port allowed */

int main(int argc, char *argv[]) {

  int sock;                   /* socket descriptor */
  char send_str[MAX_LEN];     /* string to send */
  struct sockaddr_in mc_addr; /* socket address structure */
  int send_len;               /* length of string to send */
  char* mc_addr_str;          /* multicast IP address */
  unsigned short mc_port;     /* multicast port */
  unsigned char mc_ttl=255;     /* time to live (hop count) */
  WSADATA wsaData;            /* Windows socket DLL structure */

  /* validate number of arguments */
  if (argc != 3) {
    fprintf(stderr, 
            "Usage: %s <Multicast IP> <Multicast Port>\n", 
            argv[0]);
    exit(1);
  }

  mc_addr_str = argv[1];       /* arg 1: multicast IP address */
  mc_port     = atoi(argv[2]); /* arg 2: multicast port number */

  /* validate the port range */
  if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
    fprintf(stderr, "Invalid port number argument %d.\n",
            mc_port);
    fprintf(stderr, "Valid range is between %d and %d.\n",
            MIN_PORT, MAX_PORT);
    exit(1);
  }

  /* Load Winsock 2.0 DLL */
  if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
    fprintf(stderr, "WSAStartup() failed");
    exit(1);
  }

  /* create a socket for sending to the multicast address */
  if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket() failed");
    exit(1);
  }

  /* set the TTL (time to live/hop count) for the send */
  if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, 
       (void*) &mc_ttl, sizeof(mc_ttl))) < 0) {
    perror("setsockopt() failed");
    exit(1);
  } 

  /* construct a multicast address structure */
  memset(&mc_addr, 0, sizeof(mc_addr));
  mc_addr.sin_family      = AF_INET;
  mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str);
  mc_addr.sin_port        = htons(mc_port);

  printf("Begin typing (return to send, ctrl-C to quit):\n");

  /* clear send buffer */
  memset(send_str, 0, sizeof(send_str));

  while (fgets(send_str, MAX_LEN, stdin)) {
    send_len = strlen(send_str);

    /* send string to multicast address */
    if ((sendto(sock, send_str, send_len, 0, 
         (struct sockaddr *) &mc_addr, 
         sizeof(mc_addr))) != send_len) {
      perror("sendto() sent incorrect number of bytes");
      exit(1);
    }

    /* clear send buffer */
    memset(send_str, 0, sizeof(send_str));
  }

  closesocket(sock);  
  WSACleanup();  /* Cleanup Winsock */

  exit(0);
}

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

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

发布评论

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

评论(1

月亮坠入山谷 2024-12-04 15:49:55

请查看 http://www.iana.org/assignments/多播地址/多播地址.xml

摘录

地址范围在 224.0.0.0 和 224.0.0.255 之间(含)
保留供路由协议和其他低级协议使用
拓扑发现或维护协议,例如网关发现
和小组成员报告。组播路由器不应转发
目标地址在此范围内的任何多播数据报,
无论其 TTL 为何。

具体来说,224.0.0.1 是为“此子网上的所有系统”保留的。

我不知道有任何命令可以确认您的交换机支持多播。我的经验法则是:消费者级别的开关不会。企业级交换机可以。介于两者之间的一切;谷歌是你的朋友。

上周我发现在企业级交换机中它也是可配置的(尽管我不是系统管理员,而且我不知道如何做到这一点......)

Please take a look at http://www.iana.org/assignments/multicast-addresses/multicast-addresses.xml

Excerpt

The range of addresses between 224.0.0.0 and 224.0.0.255, inclusive,
is reserved for the use of routing protocols and other low-level
topology discovery or maintenance protocols, such as gateway discovery
and group membership reporting. Multicast routers should not forward
any multicast datagram with destination addresses in this range,
regardless of its TTL.

Specifically, 224.0.0.1 is reserved for "All Systems on this Subnet".

I don't know of any command that would confirm that your switches support multicasting. My rule of thumb is: consumer level switches don't. Enterprise level switches do. Everything in between; google is your friend.

And last week I found out the hard way that in enterprise level switches it's configurable too (though I'm no sys admin and I haven't got a clue how to do that...)

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