c# xaml uwp datagram uwp-xaml

c# - UWP DatagramSocket Multicast



xaml uwp-xaml (1)

Entonces, en conclusión, ¿cómo puedo lograr, no importa cuándo (no dentro de 5 segundos) algunos de los clientes envían un mensaje, todos los demás clientes lo obtienen?

Parece que este problema se ha resuelto en el último sistema operativo Windows RS1 (Build 14393) , aquí está la captura de pantalla (Gif):

Es posible que deba actualizar el sistema operativo para resolverlo.

Logré hacer una aplicación simple que envía y recibe datos de un grupo de multidifusión. Si abro la instancia 2 de la aplicación (2 archivos .sln diferentes con el mismo código) puedo enviar y recibir datos. El problema es que después de 5 segundos, si envío un mensaje desde Client001, solo el Client001 recibirá el mensaje. Pero, si envío un mensaje desde el Client002 (la segunda instancia de la aplicación) dentro de los 5 segundos, ambos recibirán el mensaje. Tenía un ejemplo con UdpClient que funcionaba perfectamente, pero que ya no está disponible para UWP. Entonces, en conclusión, ¿cómo puedo lograr, no importa cuándo (no dentro de 5 segundos) algunos de los clientes envían un mensaje, todos los demás clientes lo obtienen?

Este es el código para 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 + "/""; }); } } }

Este es el código para 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>

ACTUALIZACIÓN: Después de buscar un poco, descubrí que tal vez el TTL (Time To Live) es el problema, pero todavía no sé cómo solucionar este problema.