本文介绍了UDP:听同一端口的两个不同的组播流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用相同端口收听2不同组播组。 程序A 230.0.0.1 听和方案B 230.0.0.2 。两个多播组使用相同的 2000端口,我有没有对其进行控制。

I need to listen to 2 different multicast groups using the same port. Program A will listen from 230.0.0.1 and Program B from 230.0.0.2. Both multicast groups use the same port 2000 and I have no control over it.

当我运行我的程序我在每个程序同时接收多播流,无论是上广播 230.0.0.1 230.0的数据包。 0.2 。我怀疑问题是由于公共端口。这是code我使用订阅播:

When I run my programs I receive both multicast streams in each program, that is both the data packets broadcasted on 230.0.0.1 and 230.0.0.2. I suspect the problem is due to the common port. This is the code I am using to subscribe to the multicast:

if( (sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0 ) {
  perror("socket");
  return -1;
}

if( setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0 ) {
  perror("setsockopt SO_REUSEADDR");
  return -1;
}

memset(&in_addr, 0, sizeof(in_addr));
in_addr.sin_family = AF_INET;
in_addr.sin_addr.s_addr = htonl(INADDR_ANY);
in_addr.sin_port = htons(2000);
if( bind(sd, (struct sockaddr*)&in_addr, sizeof(in_addr)) < 0 ) {
  perror("bind");
  return -1;
}

memset(&req, 0, sizeof(req));
inet_aton(intfc_ip, &req.imr_interface);
inet_aton("230.0.0.1", &req.imr_multiaddr);
if( setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof(req)) < 0 ) {
  perror("setsockopt IP_ADD_MEMBERSHIP");
  return -1;
}

recv()...

我怎么能在每个程序过滤特定组播组?

How can I filter a specific multicast group in each program?

推荐答案

如果您更改

in_addr.sin_addr.s_addr = htonl(INADDR_ANY);

inet_aton(<your wanted IP address>, &in_addr.sin_addr.s_addr);

你可以有更多的成功。

you could have more success.

(如果你改变你的程序使用,可以使未来的证明。)

(And if you change your program to work with getaddrinfo(), you make it future-proof.)

这篇关于UDP:听同一端口的两个不同的组播流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 07:32