服务器和客户端位于同一台计算机上时收听广播 [英] Listening to broadcasts when server and client are on the same machine

查看:74
本文介绍了服务器和客户端位于同一台计算机上时收听广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

度过了大部分时间后,我无法使其正常工作.这是我在做什么:

After spending better part of the night, I haven't been able to make it work. Here is what I'm doing:

  1. 这是一个网络游戏,由一个参与者主持,并由其他参与者加入.主持人本人也扮演玩家.
  2. 用户单击主机"按钮以开始在UDP上投放广告.
  3. 其他用户可以查看所有可用主机的列表,然后选择一个要加入的主机.

首先,我下载了 NetworkHelper库,以研究UWP中的网络工作方式.该库提供了分别用于 Host Player 类的 UDPManager UDPParticipant 类.

As a starter, I downloaded NetworkHelper library to study how networking work in UWP. This library provides UDPManager and UDPParticipant classes that I used for my Host and Player classes respectively.

该库还包含一个小的问答游戏示例.我的体系结构和游戏之间的唯一主要变化是,我的一名玩家需要同时充当主机和玩家,而示例游戏可以一次充当主机或玩家.因此,与它们不同,我需要同时监听两个 DatagramSocket 对象.

The library includes a small question/answer game sample too. The only major change between my architecture and the game is that one of my players needs to act as both Host and Player, whereas the sample game can act either as host or player at a time. So unlike them, I need to have two DatagramSocket objects listening at the same time.

一切正常,除了(grrr ...为什么此 except 总是在拐角处),在主机上运行的客户端无法收听广告消息. DatagramSocket 告诉我,同一网络地址(协议/主机/端口)不能有多种用法.如果我为服务器和客户端使用不同的端口(下面的 AdvertiserPort ListenerPort ),也没有例外,但是客户端永远不会收到广告消息.

Everything works fine, except (grrr... why this except is always around the corner) that the client running on the host machine can't listen to advertisement messages. DatagramSocket tells me that I can't have multiple usages for the same network address (protocol/host/port). If I use different ports for server and client (AdvertiserPort and ListenerPort below), there is no exception, but the client never receives the advertisement message.

这是服务器(仅包括相关代码):

Here's the server (including relevant code only):

AdvertiserSocket = new DatagramSocket();
AdvertiserSocket.MessageReceived += MessageToConnectReceivedFromParticipantAsync;
await AdvertiserSocket.BindServiceNameAsync(AdvertiserPort);

_timer = new Timer(async state => await SendMessageAsync(), null, 0, AdvertiserInterval);

和广告:

private async Task SendMessageAsync()
{
  Stream outStream = (await AdvertiserSocket.GetOutputStreamAsync(AdvertiserGroupHost, AdvertiserPort)).AsStreamForWrite();

  using (var writer = new StreamWriter(outStream))
  {
    await writer.WriteLineAsync(AdvertiserMessage);
    await writer.FlushAsync();
  }
}

这里是客户:

_listenerSocket = new DatagramSocket();
_listenerSocket.MessageReceived += AdvertisementMessageReceivedFromManagerAsync;
await _listenerSocket.BindServiceNameAsync(ListenerPort);
_listenerSocket.JoinMulticastGroup(ListenerGroupHost);

我在这里做错了什么?是否可以在同一台计算机上运行UDP广告客户和侦听器?如果是,我是否为每个端口使用相同或不同的端口?

What am I doing wrong here? Is it possible to have a UDP advertiser and listener running on the same machine? If yes, do I use same or different ports for each?

另一方面,该库将 237.1.3.37 用作 UDP_MULTICAST_IP .那是对的吗?我读过某个地方,需要使用 255.255.255.255 广播广告.是吗?

On a side note, the library uses 237.1.3.37 as UDP_MULTICAST_IP. Is that correct? I read somewhere that I need to use 255.255.255.255 for broadcasting advertisements. Yes?

推荐答案

在MS guy的帮助下解决了这个问题.这似乎是 DatagramSocket 类中的错误.开始从其他广告商接收多播数据之前,您需要在多播组上发送至少一条消息.解决方法是,您可以在开始收听之前发送一条空消息.有关更多详细信息和示例代码,请参见此SO帖子(这是该问题的绝对简化版本).

Figured it out with the help of MS guy. This seems to be a bug in DatagramSocket class. You need to send at least one message on the multicast group before you start receiving multicast data from other advertisers. As a workaround, you can send an empty message before you start listening. More details and sample code can be found on this SO post (which is an absolutely simplified version of this question).

此外,它确认了以下内容:

Additionally, it confirmed the following:

  1. 如果在广告客户套接字上将 Control.MulticastOnly 设置为 true ,则可以使用同一主机/端口使用多个套接字.
  2. 如果广告客户套接字仅在进行多播,则不需要调用 BindServiceNameAsync().
  3. 237.1.3.37 多播范围适用于多播.不需要,也不应该使用255.255.255.255.
  1. You can have more than one sockets using the same host/port if you set Control.MulticastOnly to true on the advertiser socket.
  2. Advertiser socket does not need to call BindServiceNameAsync() if it is only doing multicasting.
  3. 237.1.3.37 and any other address in the multicast range works for multicasting. 255.255.255.255 is not needed and shouldn't be used.

希望这对将来的人有帮助.

Hope this helps someone down the road.

这篇关于服务器和客户端位于同一台计算机上时收听广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆