Skip to main content

 none
Send data from MATLAB to UWP app via UDP RRS feed

  • Question

  • Hello guys,

    I tried to send a string from MATLAB to a UWP app via UDP. On MATLAB a UDP server is running and on UWP a UDP Client. When I try to send data from the client to the server it works. But when I try to send data from the server to the client no data will be received. I added the source codes beneath.

    MATLAB:

    unity_server=udp('localhost',8001,'LocalPort',8000);
    
    fopen(unity_server);
    disp(unity_server.status);
    
    
    while(1)
       
          fprintf(unity_server,'Hallo Server');
          A=fscanf(unity_server);
          disp(A)
    end


    UWP app:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using Windows.Networking;
    using Windows.Networking.Connectivity;
    using Windows.Networking.Sockets;
    using System.Diagnostics;
    
    // Die Elementvorlage "Leere Seite" wird unter https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x407 dokumentiert.
    
    namespace UDP_Client
    {
        /// <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();
                UDP_Client();
            }
    
            private async void UDP_Client()
            {
                Windows.Networking.Sockets.DatagramSocket socket = new Windows.Networking.Sockets.DatagramSocket();
    
                socket.MessageReceived += Socket_MessageReceived;
                
    
                //You can use any port that is not currently in use already on the machine. We will be using two separate and random 
                //ports for the client and server because both the will be running on the same machine.
                string serverPort = "8000";
                string clientPort = "8001";
    
                //Because we will be running the client and server on the same machine, we will use localhost as the hostname.
                Windows.Networking.HostName serverHost = new Windows.Networking.HostName("localhost");
    
                //Bind the socket to the clientPort so that we can start listening for UDP messages from the UDP echo server.
    
                 await socket.BindServiceNameAsync(clientPort);
                //await socket.BindServiceNameAsync(serverPort);
                //await socket.BindEndpointAsync(serverHost, clientPort);
               
    
                //Write a message to the UDP echo server.
                Stream streamOut = (await socket.GetOutputStreamAsync(serverHost, serverPort)).AsStreamForWrite();
                StreamWriter writer = new StreamWriter(streamOut);
                string message = "Hello, world!";
                await writer.WriteLineAsync(message);
                await writer.FlushAsync();
    
                
    
    
    
            }
    
            private async void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender,
                Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
            {
                //Read the message that was received from the UDP echo server.
                Stream streamIn = args.GetDataStream().AsStreamForRead();
                StreamReader reader = new StreamReader(streamIn);
                string message = await reader.ReadLineAsync();
    
            }
        }
    }
    

    Does anybody know a solution for that issue?

    Greetings,

    Chris

    Tuesday, November 21, 2017 6:19 PM