UWP DatagramSocket Multicast

心已入冬 提交于 2019-12-02 03:12:49

问题


I managed to make a simple application that sends and receives data from a multicast group. If I open 2 instance of the application (2 different .sln files with the same code) I can send and receive data. The problem is that after 5 seconds, if I send a message from the Client001 only the Client001 will get the message. But, if I send message from the Client002 (the second instance of the app) within the 5 seconds then both of them get the message. I had an example with UdpClient that was working perfectly, but that is no longer available for UWP. So in conclusion, how can I achieve, no matter when (not within 5 seconds) some of the clients sends a message, all of the other clients get it?

This is the code for the MainPage.xaml.cs

namespace Client001
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private DatagramSocket listenerSocket = null;
        public string remoteAddress = "224.3.0.5";
        HostName remoteHostname;
        public string serviceName = "22113";
        IOutputStream outputStream;
        DataWriter writer;

        public MainPage()
        {
            this.InitializeComponent();
            SetupMulticastScenarioUI();

            remoteHostname = new HostName(RemoteAddress.Text);
        }

        private void CloseListenerSocket()
        {
            if (listenerSocket != null)
            {
                // DatagramSocket.Close() is exposed through the Dispose() method in C#.
                // The call below explicitly closes the socket, freeing the UDP port that it is currently bound to.
                listenerSocket.Dispose();
                listenerSocket = null;
            }
        }

        // Sets up the UI to display the multicast scenario options.
        private void SetupMulticastScenarioUI()
        {
            RemoteAddress.Text = remoteAddress;
            ServiceName.Text = serviceName;
            StartListener.Content = "Start listener and join multicast group";
            SendMessageButton.IsEnabled = false;
            CloseListenerButton.IsEnabled = false;
            SendOutput.Text = "";
        }

        private async void StartListener_Click(object sender, RoutedEventArgs e)
        {
            listenerSocket = new DatagramSocket();

            listenerSocket.Control.MulticastOnly = true;
            await listenerSocket.BindServiceNameAsync(ServiceName.Text);

            // Join the multicast group to start receiving datagrams being sent to that group.
            listenerSocket.JoinMulticastGroup(remoteHostname);

            listenerSocket.MessageReceived += MessageReceived;
            SendOutput.Text = "Listening on port " + listenerSocket.Information.LocalPort + " and joined to multicast group";

            // Enable scenario steps that require us to have an active listening socket.
            SendMessageButton.IsEnabled = true;
            CloseListenerButton.IsEnabled = true;

            outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, ServiceName.Text);
            writer = new DataWriter(outputStream);
            writer.WriteString("Handshake1");
            await writer.StoreAsync();
        }

        private async void SendMessage_Click(object sender, RoutedEventArgs e)
        {
            writer.WriteString(Message.Text);
            await writer.StoreAsync();
        }

        private void CloseListener_Click(object sender, RoutedEventArgs e)
        {
            CloseListenerSocket();

            // Disable scenario steps that require us to have an active listening socket.
            SendMessageButton.IsEnabled = false;
            CloseListenerButton.IsEnabled = false;
            SendOutput.Text = "";

            SendOutput.Text = "Listener closed";
        }

        async void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
        {
            // Interpret the incoming datagram's entire contents as a string.
            uint stringLength = eventArguments.GetDataReader().UnconsumedBufferLength;
            string receivedMessage = eventArguments.GetDataReader().ReadString(stringLength);

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                SendOutput.Text = "Received data from remote peer (Remote Address: " +
                    eventArguments.RemoteAddress.CanonicalName + ", Remote Port: " +
                    eventArguments.RemotePort + "): \"" + receivedMessage + "\"";
            });
        }
    }
}

This is the code for the MainPage.xaml

<Page
    x:Class="Client001.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Client001"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">


    <StackPanel>
        <TextBlock>Remote Address:</TextBlock>
        <TextBox x:Name="RemoteAddress" />

        <TextBlock>Service Name:</TextBlock>
        <TextBox x:Name="ServiceName" />

        <Button x:Name="StartListener" Click="StartListener_Click" Margin="0,10,0,0"/>
        <Button x:Name="SendMessageButton" Content="Send 'hello'" Click="SendMessage_Click" Margin="0,10,0,0"/>
        <Button x:Name="CloseListenerButton" Content="Close Listener" Click="CloseListener_Click" Margin="0,10,0,0"/>
        <TextBlock x:Name="SendOutput" TextWrapping="Wrap"  Margin="0,10,0,0"/>
        <TextBox x:Name="Message"></TextBox>
    </StackPanel>

</Page>

UPDATE: After a bit of searching i found out that maybe the TTL(Time To Live) is the problem, but I still don't know how to fix this issue.


回答1:


So in conclusion, how can I achieve, no matter when (not within 5 seconds) some of the clients sends a message, all of the other clients get it?

It seems like that this issue has been fixed in the latest Windows RS1(Build 14393) OS, here is the screenshot(Gif):

You may need to upgrade the OS to solve it.



来源:https://stackoverflow.com/questions/38679646/uwp-datagramsocket-multicast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!