(UWP)和WPF之间的(UDP)通信

时间:2019-03-21 09:09:03

标签: c# wpf uwp udp communication

我在同一台计算机上运行了两个应用程序(一个UWP和一个WPF),它们应进行如下通信: WPF应用程序从USB端口读取数据并将数据发送到UWP。 UWP应用会获取数据并使用它。

现在,我一直在尝试使用UDP作为通信通道来实现此目的,但是UWP应用程序什么也没收到。 如果我创建两个UWP应用程序,它们可以通过UDP进行通信而不会出现问题。不幸的是,我需要WPF之一!

我很确定这是一个非常普遍的情况,应该有一个简单的解决方案,但是我已经寻找了好几天没有找到解决方案。

顺便说一句,我发现了一个similar question,但这是关于C ++的,而我需要一个C#解决方案。

编辑:

这是一些最低限度的运行示例,也许有助于阐明这个问题。

UWP侦听器

using System;
using System.Diagnostics;
using System.IO;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UDP1_UWP
{
    public sealed partial class DatagramSocketPage : Page
    {
        public DatagramSocketPage()
        {
            this.InitializeComponent();
        }

        static string ClientPortNumber = "1336";
        static string ServerPortNumber = "1337";

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.StartServer();
        }

        private async void StartServer()
        {
            try
            {
                var serverDatagramSocket = new Windows.Networking.Sockets.DatagramSocket();


                serverDatagramSocket.MessageReceived += ServerDatagramSocket_MessageReceived;

                this.serverListBox.Items.Add("server is about to bind...");


                await serverDatagramSocket.BindServiceNameAsync(DatagramSocketPage.ServerPortNumber);

                this.serverListBox.Items.Add(string.Format("server is bound to port number {0}", DatagramSocketPage.ServerPortNumber));
            }
            catch (Exception ex)
            {
                Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
                this.serverListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message);
            }
        }

        private async void ServerDatagramSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
        {
            string request;
            using (DataReader dataReader = args.GetDataReader())
            {
                request = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim();
            }

            await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.serverListBox.Items.Add(string.Format("server received the request: \"{0}\"", request)));

            // Echo the request back as the response.
            using (Stream outputStream = (await sender.GetOutputStreamAsync(args.RemoteAddress, DatagramSocketPage.ClientPortNumber)).AsStreamForWrite())
            {
                using (var streamWriter = new StreamWriter(outputStream))
                {
                    await streamWriter.WriteLineAsync(request);
                    await streamWriter.FlushAsync();
                }
            }

            await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.serverListBox.Items.Add(string.Format("server sent back the response: \"{0}\"", request)));


        }

    }
}

WPF发件人 我试图使用WinRT库,以便两个应用程序都可以使用相同的类。为此,我

  1. 创建了一个空的WPF项目
  2. 添加了对C:\ Program Files(x86)\ Windows的引用 套件\ 10 \ UnionMetadata \ 10.0.17763.0 \ Windows.winmd和C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETCore \ v4.5 \ System.Runtime.WindowsRuntime.dll

现在我可以在WPF应用程序中使用以下名称空间:

  • 使用Windows.Networking;
  • 使用Windows.Networking.Sockets;
  • 使用Windows.Storage.Streams;

代码如下:

Program.cs

using System;
using XSensUDPSender.UDP;

namespace XSensUDPServer
{
    class Program
    {

        static void Main(string[] args)
        {
            UDPClient c = new UDPClient();
            c.StartClientAsync();

            string text;
            do
            {
                Console.WriteLine("Message to send: ");
                text = Console.ReadLine();
                c.SendMessageAsync(text);
            } while (text != "quit");

        }
    }
}

UDPClient.cs

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;

namespace XSensUDPSender.UDP
{
    class UDPClient
    {
        private DatagramSocket clientDatagramSocket;
        private HostName hostName;
        private string ClientPortNumber = "1336";
        private string ServerPortNumber = "1337";

        public UDPClient(string aClientPort = "1336", string aServerPort = "1337")
        {
            ClientPortNumber = aClientPort;
            ServerPortNumber = aServerPort;
        }

        public async void StartClientAsync()
        {
            try
            {
                // Create the DatagramSocket and establish a connection to the echo server.
                clientDatagramSocket = new Windows.Networking.Sockets.DatagramSocket();

                clientDatagramSocket.MessageReceived += ClientDatagramSocket_MessageReceived;

                // The server hostname that we will be establishing a connection to. In this example, the server and client are in the same process.
                hostName = new Windows.Networking.HostName("localhost");

                Console.WriteLine("CLIENT is about to bind...");
                await clientDatagramSocket.BindServiceNameAsync(ClientPortNumber);
                Console.WriteLine(string.Format("CLIENT is bound to port number {0}", ClientPortNumber));


            }
            catch (Exception ex)
            {
                Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
                Debug.WriteLine(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message);
            }
        }

        private void ClientDatagramSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
        {
            string response;
            using (DataReader dataReader = args.GetDataReader())
            {
                response = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim();
                Console.WriteLine("CLIENT RECEIVED: " + response);
            }

        }


        public async Task SendMessageAsync(string request)
        {
            // Send a request to the echo server.
            using (var serverDatagramSocket = new Windows.Networking.Sockets.DatagramSocket())
            {
                using (Stream outputStream = (await serverDatagramSocket.GetOutputStreamAsync(hostName, ServerPortNumber)).AsStreamForWrite())
                {
                    using (var streamWriter = new StreamWriter(outputStream))
                    {
                        await streamWriter.WriteLineAsync(request);
                        await streamWriter.FlushAsync();
                    }
                }
            }


        }

    }
}

1 个答案:

答案 0 :(得分:0)

@ XavierXie-MSFT不是重复的。我实际上找到了答案:

  

由于网络隔离,Windows禁止建立   两个运行的UWP应用之间的套接字连接(套接字或WinSock)   在同一台机器上;是否通过本地环回地址   (127.0.0.0),或通过显式指定本地IP地址。对于   有关UWP应用程序可以与之通信的机制的详细信息   另请参阅应用程序间通信。

来源:https://docs.microsoft.com/en-us/windows/uwp/networking/sockets

因此,显然,这是“功能”,而不是错误。尽管,恕我直言,微软还是再次发动了攻击。他们怎么想阻止本地主机请求!?

无论如何,要解决该问题,我找到了另一种解决方案:我正在使用App Services (请检查https://docs.microsoft.com/en-us/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service)。 所以,现在我有了包含后台应用程序服务的UWP应用程序。

整个基础架构可以想象如下: UWP <->后台应用程序服务<-> WPF

我受此启发:https://csharp.christiannagel.com/2018/10/09/desktopbridge/

UWP应用程序实例化一个AppServiceConnection到后台服务。 WPF应用程序还实例化到后台服务的AppServiceConnection。 后台服务有两个AppServiceConnection:一个绑定到UWP应用,另一个绑定到WPF应用。

当UWP(作为WPF)与WPF(作为UWP)进行通信时,它将消息发送到后台服务。后台服务将消息转发到WPF(分别为UWP),获取响应并将响应转发回UWP(分别为WPF)。

现在,我遇到了一个新问题:WPF的AppServiceConnection成功向UWP发送了226条消息后被关闭。但是,WPF继续从UWP接收消息。 也许这篇文章可以帮助我解决这个新问题(我会及时通知您): UWP AppServiceConnection - SendResponseAsync returns AppServiceResponseStatus.Failure