Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UDP广播发送在UWP中不工作_C#_Sockets_Uwp - Fatal编程技术网

C# UDP广播发送在UWP中不工作

C# UDP广播发送在UWP中不工作,c#,sockets,uwp,C#,Sockets,Uwp,我尝试使用基于的UWP创建UDP发送和侦听应用程序 它应该是这样工作的: 在端口8080上侦听消息 向广播地址发送消息 从另一个UDP服务器接收消息,该服务器侦听发送到广播的消息 我有Listen和Send方法 构造函数 public MainViewModel() { Listen(); Send(); } 听 private async void Listen() { listenerSocket = new DatagramSocket();

我尝试使用基于的UWP创建UDP发送和侦听应用程序

它应该是这样工作的:

  • 在端口8080上侦听消息
  • 向广播地址发送消息
  • 从另一个UDP服务器接收消息,该服务器侦听发送到广播的消息
我有
Listen
Send
方法

构造函数

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()
{
   ...
}

}

您可以发布您的
GetBroadcastAddress(localIP,subnetIP)
code吗?我想问题可能就在这里,我测试了你的代码,但改为使用localaddress,它成功了。