none
Network connection between UWP Apps RRS feed

  • Frage

  • I got two Windows UWP Apps. One of them (the "server") is running on a Raspberry Pi 2 on Windows IoT (10586.0). The other (the "client") is running on any Windows 10 device within the same network.

    What I want is to get the apps to "talk" to each other. For the moment I just want to send simple String from the client to the server. Later on, serialized data should be transferred trough the network.

    This is the code for the server App:

    namespace LCARSHomeAutomation
    {
        /// <summary>
        /// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
                try {
                    EstablishNetworking();
                    txb_Events.Text += "Server Running";
                }catch (Exception ex)
                {
                    txb_Events.Text += ex.Message;
                }
    
                
            }
    
            private async void EstablishNetworking()
            {
                await StartListener();
            }
    
            public async Task StartListener()
            {
                StreamSocketListener listener = new StreamSocketListener();
                listener.ConnectionReceived += OnConnection;
    
                listener.Control.KeepAlive = true;
    
                try
                {
                    await listener.BindServiceNameAsync("5463");
                    
                }
                catch (Exception ex)
                {
                    if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown)
                    {
                        throw;
                    }
                    //Logs.Add(ex.Message);
                    txb_Events.Text += ex.Message;
                }
    
            }
    
            private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
            {
                //Stream inStream = args.Socket.InputStream.AsStreamForRead();
                //StreamReader reader = new StreamReader(inStream);
                //string request = await reader.ReadLineAsync();
                DataReader reader = new DataReader(args.Socket.InputStream);
                uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
                if (sizeFieldCount != sizeof(uint))
                {
                    return;
                }
                
                uint stringLength = reader.ReadUInt32();
                uint actualStringLength = await reader.LoadAsync(stringLength);
    
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                           () =>
                                           {
                                           // Your UI update code goes here!
                                           txb_Events.Text += reader.ReadString(actualStringLength);//request;
                                           });
    
    
            }
    
            private async Task ConnectSocket()
            {
                StreamSocket socket = new StreamSocket();
    
                socket.Control.KeepAlive = false;
    
                HostName host = new HostName("localhost");
    
                try
                {
                    await socket.ConnectAsync(host, "5463");
    
                    //Stream streamOut = socket.OutputStream;
                    DataWriter writer = new DataWriter(socket.OutputStream);
                    string request = "Test Self App \n";
                    writer.WriteUInt32(writer.MeasureString(request));
                    writer.WriteString(request);
    
                    await writer.StoreAsync();
    
                    writer.DetachStream();
                    writer.Dispose();
    
                    //await writer.WriteLineAsync(request);
                    //await writer.FlushAsync();
    
                    //socket.Dispose();
                }
                catch (Exception ex)
                {
                    txb_Events.Text += ex.Message;
                    //Logs.Add(ex.Message)
                }
    
    
            }
    
            private async void btn_Send_Click(object sender, RoutedEventArgs e)
            {
                await ConnectSocket();
            }
            
        }
    }

    As you can see, I already changed from StreamReader / -Writer to DataReader / -Writer.

    I'm establishing a network connection with the same app on the same host and send the string "Test Self App". This works fine for quite some time but after a while I get the Error:

    Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll

    WinRT information: No connection could be made because the target machine actively refused it.

    So, this is my first question: What is this Error and how can I fix this?

    The other thing is: I'm not able to establish a network Connection between the server and the Client. I don't know, what I am doing wrong. This is the code of the "Client":

    namespace LCARSRemote
    {
        /// <summary>
        /// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private async void btn_Send_Click(object sender, RoutedEventArgs e)
            {
                StreamSocket socket = new StreamSocket();
    
                HostName host = new HostName("localhost"); //Replace with correct hostname when Server running on RPi
    
                try
                {
                    try
                    {
                        await socket.ConnectAsync(host, "5463");
                    }
                    catch (Exception ex)
                    {
                        txb_Events.Text += ex.Message;
                        txb_Events.Text += " <-Inner Catch";
                    }
    
                    //Stream streamOut = socket.OutputStream.AsStreamForWrite();
                    //StreamWriter writer = new StreamWriter(streamOut);
                    //string request = "Remote App Test";
                    //await writer.WriteLineAsync(request);
                    //await writer.FlushAsync();
    
                    //socket.Dispose();
    
                    DataWriter writer = new DataWriter(socket.OutputStream);
                    string request = "Test Remote App \n";
                    writer.WriteUInt32(writer.MeasureString(request));
                    writer.WriteString(request);
    
                    await writer.StoreAsync();
    
                    writer.DetachStream();
                    writer.Dispose();
    
                }
                catch (Exception ex)
                {
                    txb_Events.Text += ex.Message;
                    txb_Events.Text += " <-Outer Catch";
                }
    
            }
        }
    }

    When I click on the btn_Send, I get the error message

    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

    and

    A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)

    What am I doing wrong? Maybe I should say, that I'm relatively new in programming network connections, sockets etc.

    Thanks for any help!

    Freitag, 8. April 2016 10:23

Alle Antworten