none
关于DatagramSocket的udp广播问题 RRS feed

  • 问题

  • 自己写了一个udp广播的程序,本机通讯可以完成,但将发送端的ip地址改为192.168.1.255或192.168.255.255之后,udpClient_receive_MessageReceived事件不会被触发

    下面是接收端代码

    try
    {
            udpClient_receive = new DatagramSocket();
            udpClient_receive.MessageReceived +=     udpClient_receive_MessageReceived;
            await udpClient_receive.BindServiceNameAsync("11000");
    }
    catch(Exception ex)
    {
            throw ex;
    }
    
    
    
    async void udpClient_receive_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
    {
          IBuffer buffer;
          try
          {
               IOutputStream outputStream = await udpClient_receive.GetOutputStreamAsync(args.RemoteAddress,args.RemotePort);
               buffer = args.GetDataReader().DetachBuffer();
               byte[] bytes = new byte[1024];
               WindowsRuntimeBufferExtensions.CopyTo(buffer,bytes);
                    
               string str;
               str = Encoding.Unicode.GetString(bytes,0,bytes.Length);               
          }
          catch(Exception ex)
          {
               throw ex;
          }
          //throw new NotImplementedException();
    }

    发送端代码

    try
    {
            HostName remoteHost = new HostName("127.0.0.1");
            udpClient_send = new DatagramSocket();
                    
            await udpClient_send.ConnectAsync(remoteHost, "11000");
            DataWriter writer = new DataWriter(udpClient_send.OutputStream);
            Byte[] sendBytes = Encoding.Unicode.GetBytes(sBox.Text);
            writer.WriteBytes(sendBytes);
            await writer.StoreAsync();
            writer.DetachStream();
            writer.Dispose();
    }
    catch(Exception ex)
    {
            throw ex;
    }
    将127.0.0.1改为192.168.1.255之后程序不能正常执行,本人小白,希望各位大神能够指点一下,不知道metro应用的广播是否采用的是这种格式

    2012年11月3日 10:33

答案