title: Unable to listen UDP port in C#
body: <p>I am trying to listen to UDP port 10086, store the data in the string &quot;loggingEvent&quot;, combine the string and send it to the UDP port 51999. The UDP client 10086 is used to listen to port 10086 and the Udpclient 10087 is for sending the message to port 51999.</p>
  <p>Although I can receive data in the UDP tool &quot;Packet Sender&quot;, the C# code failed to listen to the message on port 10086. However, the port &quot;10086&quot;, &quot;10087&quot; and &quot;51999&quot; really works, which can be found by using the command &quot;net-stat -ano&quot;.
  <a href="https://i.stack.imgur.com/GycXU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GycXU.jpg" alt="Packet Sender" /></a></p>
  <p><a href="https://i.stack.imgur.com/Q2PuR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q2PuR.jpg" alt="net-stat" /></a> After debugging, I found that the thread exited at the line &quot;var udpResult = await udpClient_listen.ReceiveAsync();&quot;, which confused me a lot. I have also tried non-asynchronous and still does not work. However, the &quot;SendAsync&quot; function works well.</p>
  <p>In &quot;CharacteristicPage.xaml.cs&quot;</p>
  <pre><code>   public static string udp_listener_ip;
     public static int udp_listener_port;
     public static int udp_client_port;
     public static string udp_message;
     public static UdpClient udpClient_listen;
     public static UdpClient udpClient_send;
     public static string loggingEvent; 
   
     private void button_udp_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
     {
          IPEndPoint endPoint_listen= new IPEndPoint(IPAddress.Parse(udp_listener_ip), 10086);
          udpClient_listen = new UdpClient(endPoint_listen);
  
          IPEndPoint endPoint1 = new IPEndPoint(IPAddress.Parse(udp_listener_ip), 10087);
          udpClient_send = new UdpClient(endPoint1);
  
          UDPListener();
      }
  
      private static async Task UDPListener()
      {
          try
          {
              while (true)
              {
                  Debug.WriteLine(&quot;###############UDP listener task start###############&quot;);                   
                  var udpResult = await udpClient_listen.ReceiveAsync();
                  loggingEvent = Encoding.ASCII.GetString(udpResult.Buffer);
                  Debug.WriteLine(&quot;UDP listener received: &quot; + loggingEvent);
              }
          }
  
          catch (Exception e)
          {
              Debug.WriteLine(e.Message);
              var messageDialog = new MessageDialog(e.Message, &quot;UDP connect failures&quot;);
              await messageDialog.ShowAsync();
          }
      }
  </code></pre>
  <p>In &quot;ObservableGattCharacteristic.cs&quot;</p>
  <pre><code>   private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
      {
          await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
              Windows.UI.Core.CoreDispatcherPriority.Normal,
              () =&gt;
          {
              SetValue(args.CharacteristicValue);
          });
         
          var udpClient_send = Views.CharacteristicPage.udpClient_send;
          var loggingEvent = Views.CharacteristicPage.loggingEvent;
  
          try
          {
              IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(Views.CharacteristicPage.udp_listener_ip), 51999);
  
              DateTime foo = DateTime.Now;
              long unixTime = ((DateTimeOffset)foo).ToUnixTimeSeconds();
  
              byte[] Payload = Encoding.UTF8.GetBytes(unixTime.ToString() + &quot;,&quot; + loggingEvent + &quot;,&quot; + ParseValue(value));
  
              await udpClient_send.SendAsync(Payload, Payload.Length, endPoint);
              Debug.WriteLine(&quot;UDP send message: &quot; + Encoding.UTF8.GetString(Payload));
          }
  
          catch
          {
              Debug.WriteLine(&quot;FAILED to publish message!&quot; );
          }
      }
  </code></pre>