通过插座进行最简单的广告/收听安排 [英] Simplest possible advertise/listen arrangement through sockets

查看:73
本文介绍了通过插座进行最简单的广告/收听安排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下简单安排有什么问题.我要做的就是创建一个UDP广告客户来多播一条消息,以及一个侦听器加入多播组以接收此消息,它们都在同一台机器上运行.

What's wrong with the following simple arrangement. All I'm doing is to create a UDP advertiser that multicasts a message, and a listener that joins the multicast group to receive this message, both running on the same machine.

string Port = "54153";
HostName Host = new HostName("224.3.0.5"); //a multicast range address

//listener
var L = new DatagramSocket();
L.MessageReceived += (sender2, args) => { /*something*/ };
await L.BindServiceNameAsync(Port);
L.JoinMulticastGroup(Host);

//advertiser
var AdvertiserSocket = new DatagramSocket();
AdvertiserSocket.Control.MulticastOnly = true;

Stream outStream = (await AdvertiserSocket.GetOutputStreamAsync(Host, Port)).AsStreamForWrite();
using (var writer = new StreamWriter(outStream))
{
  await writer.WriteLineAsync("MESSAGE");
  await writer.FlushAsync();
}

侦听器根本不接收任何内容(MessageReceived从不调用).我尝试了以下变体而没有成功:

The listener doesn't receive anything at all (MessageReceived never invoked). I have tried the following variations without success:

  1. 在广告客户上调用和不调用BindServiceNameAsync().
  2. 在广告客户和/或收听者上同时使用MulticastOnly
  3. 先创建一个对象,然后等待几秒钟.
  4. 使用255.255.255.255作为主机.
  1. Calling and not calling BindServiceNameAsync() on advertiser.
  2. Using MulticastOnly on advertiser, listener or both
  3. Waiting for a few seconds after creating one object before the other.
  4. Using 255.255.255.255 as host.

推荐答案

广告客户似乎正在正确地多播数据(TCPView显示已发送的数据包),但是接收端口却没有得到任何东西.

It seems like the advertiser is multicasting data correctly (TCPView shows sent packets), but the receiving port is not getting anything.

感谢您分享此问题.我做了一个演示并做了一些测试.我发现侦听器套接字在该组中发送了一条消息之前不会收到任何消息.

Thank you for sharing this problem. I made a demo and did some tests. I found out the listener socket won't receive any message until it has sent one message in the group.

因此,当前的解决方法是在注册收听后立即发送空消息:

So, currently the workaround is to send an empty message immediately after register for listening:

private async void btnListen_Click(object sender, RoutedEventArgs e)
{
        socket = new DatagramSocket();
        socket.MessageReceived += Socket_MessageReceived;
        socket.Control.MulticastOnly = true;
        await socket.BindServiceNameAsync(serverPort);
        socket.JoinMulticastGroup(serverHost);
        SendWithExistingSocket(socket, "");//send an empty message immediately
}

private async void SendWithExistingSocket(DatagramSocket socket, String text)
{
    if (socket != null)
    {
        Stream stream = (await socket.GetOutputStreamAsync(serverHost, serverPort)).AsStreamForWrite();
        using (var writer = new StreamWriter(stream))
        {
            writer.WriteLine(text);
            await writer.FlushAsync();
        }
    }
}

关于此问题的根本原因,我将与相关团队协商,并在得到回复后告知您.

As for the root cause of this problem, I'll consult with related team and let you know once I got response.

这篇关于通过插座进行最简单的广告/收听安排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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