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

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

问题描述

在度过了大部分夜晚之后,我一直无法让它发挥作用.这是我正在做的:

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 中的工作原理.这个库提供了 UDPManagerUDPParticipant 类,我分别用于我的 HostPlayer 类.

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 告诉我不能对同一个网络地址(协议/主机/端口)有多种用法.如果我为服务器和客户端使用不同的端口(下面的AdvertiserPortListenerPort),也不例外,但客户端永远不会收到广告消息.

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 的帮助下解决了这个问题.这似乎是 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天全站免登陆