簡體   English   中英

UDP 廣播發送在 UWP 中不起作用

[英]UDP broadcast send not working in UWP

我嘗試基於此源代碼使用 UWP 創建 UDP 發送和偵聽應用程序。

它應該像這樣工作:

  • 監聽 8080 端口的消息
  • 發送消息到廣播地址
  • 從另一個 UDP 服務器接收消息,該服務器偵聽發送到廣播的消息

我有ListenSend方法。

構造函數

public MainViewModel()
{
   Listen();
   Send();
}

private async void Listen()
    {
      listenerSocket = new DatagramSocket();
      listenerSocket.MessageReceived += (x, y) =>
      {
        var a = "2";
      };
      await listenerSocket.BindServiceNameAsync("8080");
    }

發送

private async void Send()
    {
      IOutputStream outputStream;
      string localIPString = GetLocalIp();
      IPAddress localIP = IPAddress.Parse(localIPString);
      string subnetMaskString = "255.0.0.0";
      IPAddress subnetIP = IPAddress.Parse(subnetMaskString); 
      HostName remoteHostname = new HostName(GetBroadcastAddress(localIP, subnetIP).ToString());
      outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, "8080");      
      DataWriter writer = new DataWriter(outputStream);
      writer.WriteString(localIPString);
      await writer.StoreAsync();
    }

似乎廣播發送不起作用。

我有另一個程序(用 Java 編寫),它也向廣播地址發送一條消息,這個偵聽器接收從廣播發回的消息。

我應該設置什么才能使發送工作?

我在這里沒有看到任何GetBroadcastAddress(localIP, subnetIP)代碼。 但正如我所見,您的問題可能與此代碼有關:

HostName remoteHostname = new HostName(GetBroadcastAddress(localIP, subnetIP).ToString());

我把它改成:

    HostName remoteHostname = new HostName(localIP.ToString());
    outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, port);

這是我的示例代碼:

public sealed partial class MainPage : Page
{
    DatagramSocket listenerSocket = null;
    const string port = "8080";
    public MainPage()
    {
        this.InitializeComponent();
        Listen();
        Send();
    }


private async void Listen()
{
    listenerSocket = new DatagramSocket();
    //listenerSocket.MessageReceived += (x, y) =>
    //{
    //    var a = "2";
    //};
    listenerSocket.MessageReceived += MessageReceived;
    await listenerSocket.BindServiceNameAsync(port);
}

private async void Send()
{
    IOutputStream outputStream;
    string localIPString = GetLocalIp();
    IPAddress localIP = IPAddress.Parse(localIPString);
    string subnetMaskString = "255.0.0.0";
    IPAddress subnetIP = IPAddress.Parse(subnetMaskString);
    HostName remoteHostname = new HostName(localIP.ToString());
    outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, port);

    using (DataWriter writer = new DataWriter(outputStream))
    {
        writer.WriteString("aaaa");
        await writer.StoreAsync();
    }


}

//private object GetBroadcastAddress(IPAddress localIP, IPAddress subnetIP)
//{
//    throw new NotImplementedException();
//}

async void MessageReceived (DatagramSocket socket, DatagramSocketMessageReceivedEventArgs args)
{
    DataReader reader = args.GetDataReader();
    uint len = reader.UnconsumedBufferLength;
    string msg = reader.ReadString(len);

    string remoteHost = args.RemoteAddress.DisplayName;
    reader.Dispose();

    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        text.Text = msg;
    });

}

private string GetLocalIp()
{
   ...
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM