Simplest possible advertise/listen arrangement through sockets

前端 未结 1 1855
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 14:11

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 re

相关标签:
1条回答
  • 2021-01-25 14:37

    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.

    0 讨论(0)
提交回复
热议问题