title: Problems with receiving UDP messages in Metro-Apps
body: <p>I'm currently working on a project and one of the featured devices is a Windows Tablet. To "connect" it to other devices (like some Raspberry Pi) in the project environment UDP is used to send messages. The Windows Tablet is intended to be some controlling device with soem touch functionality. Therefore I'm writing an App (and the intention of the App is not to put it into the Windows Store). The UDP part in this work is quite painful because I had to do much research since I started with no experience in App programming. More painful than the programming is, that I practically finished the work only to start over again because the App didn't receive UDP anymore. </p>
  
  <p>Here's my code (I removed elements not relevant to the actual problem). I apologize for the bad coding....</p>
  
  <p>App.xaml.cs:</p>
  
  <pre><code>sealed partial class App : Application
  {
      NetworkInterface ni = new NetworkInterface();
  
      public App()
      {
          this.InitializeComponent();
          this.Suspending += OnSuspending;
  
          ni.MessageReceived += OnMessageReceived;
  
          ni.Connect(new HostName("127.0.0.1"), "5556");
  
      }
  
      private void OnMessageReceived(object sender, MessageReceivedEventArgs e)
      {
          Debug.WriteLine("Processing");
          Debug.WriteLine(e.Message.Data);
  
      }
  
      public static new App Current
      {
          get { return Application.Current as App; }
      }
  
      private DatagramSocket _socket;
  
      protected override void OnLaunched(LaunchActivatedEventArgs e)
      {
          Update_Timer();
      }
      DispatcherTimer timer = new DispatcherTimer();
      private void Update_Timer()
      {
          timer.Start();
          timer.Interval = new TimeSpan(0,0,0,0,500);
          timer.Tick += alive;
      }
  
      private void alive(object sender, object e)
      {
          if (start == 0) {
              Debug.WriteLine("App-Startup");
              ni.SendMessage("Startup...");
              start++;
          }
          else
          {
              Debug.WriteLine("App-Alive");
              ni.SendMessage("alive");
              start++;
          }
  
      }    
  }
  </code></pre>
  
  <p>This part of code is to send and receive Messages in the backgrond in the whole App.</p>
  
  <p>And a NetworkInterface class:</p>
  
  <pre><code>    class NetworkInterface
  {
      private DatagramSocket _socket;
  
      public bool IsConnected { get; set; }
  
      public NetworkInterface()
      {
          IsConnected = false;
          _socket = new DatagramSocket();
          _socket.MessageReceived += OnSocketMessageReceived;
      }
  
      public async void Connect(HostName remoteHostName, string remoteServiceNameOrPort)
      {
          if (IsConnected != true)
          {
              await _socket.BindServiceNameAsync("5321");
              await _socket.ConnectAsync(remoteHostName, remoteServiceNameOrPort);
          }
          IsConnected = true;
      }
  
      public void alive(object sender, object e)
      {
          Debug.WriteLine("alive");
      }
  
  
      public event EventHandler&lt;MessageReceivedEventArgs&gt; MessageReceived;
  
      private void OnSocketMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
      {
          var reader = args.GetDataReader();
          var count = reader.UnconsumedBufferLength;
          var data = reader.ReadString(count);
          Debug.WriteLine(args);
          if (MessageReceived != null)
          {
              var ea = new MessageReceivedEventArgs();
              ea.Message = new Message() { Data = data };
              ea.RemoteHostName = args.RemoteAddress;
              ea.RemotePort = args.RemotePort;
              MessageReceived(this, ea);
          }
      }
  
      DataWriter _writer = null;
      public async void SendMessage(string message)
      {
          if (_writer == null)
          {
              var stream = _socket.OutputStream;
              _writer = new DataWriter(stream);
          }
  
          _writer.WriteString(message);
          await _writer.StoreAsync();
      }
  
  }
  </code></pre>
  
  <p>The main problems are:</p>
  
  <ol>
  <li>If I dont send something before receiving, I won't be able top get an message.</li>
  <li><p>If I send before I have random Faults at this line:</p>
  
  <pre><code>var reader = args.GetDataReader();
  </code></pre></li>
  <li><p>If nothing fails, I'm not able to receive messages from a local Python script (which works) but I can send messages from a local program which the App receives.</p></li>
  </ol>
  
  <p>Does anyone know how I can fix these problems?</p>
  
  <p>Thanks in advance!</p>