title: How to get actual data from UDP packets c# receiving
body: <p>I have a small program to receive UPD Packets. I have some bit problem in receiving packets . I actually need to revce packets by id for example  "position (packet id: 196) , Location :187 etc" With my code i get some data but i  dont know how to find the id and how to get real data inside the packet.</p>
  
  <p>Packet  Header </p>
  
  <p>|SYNC Byte|SYNC Byte|Data Length|Identifier|Data Bytes|Checkssum|</p>
  
  <p>|1(0x24)  |2(0x40)  |Byte       |Bye       |          |Byete    |</p>
  
  <p>Field :Sync Bytes          SIZE: 2 Bytes     </p>
  
  <p>Field :Data Length       SIZE:  1 Byte      </p>
  
  <p>Field :Packet Identifier   SIZE: 1 byte      </p>
  
  <p>Field :Data               SIZE: 0-255 bytes<br>
  Field :Checksum           SIZE: 1 byte    </p>
  
  <pre><code>enter code here
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Net.Sockets;
  using System.Net;
  using System.IO;
  
  
  namespace SimpleUdpReciever
  {
      class pProgram
      {
          static void Main(string[] args)
          {
              int localPort = 18006;
              IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 0);
              bool flag = false;
  
              for (int i = 0; i &lt; args.Length; i++)
              {
                  string cmd = args[i];
                  string value;
                  int tempInt;
                  IPAddress tempAddress;
  
                  switch (cmd)
                  {
                      case "-lp":
                          value = GetValue(args, ref i);
                          if (int.TryParse(value, out tempInt))
                              localPort = tempInt;
                          break;
                      case "-rp":
                          value = GetValue(args, ref i);
                          if (int.TryParse(value, out tempInt))
                              remoteSender.Port = tempInt;
                          break;
                      case "-rh":
                          value = GetValue(args, ref i);
                          if (IPAddress.TryParse(value, out tempAddress))
                              remoteSender.Address = tempAddress;
                          else if (int.TryParse(value, out tempInt) &amp;&amp; tempInt == 0)
                              remoteSender.Address = IPAddress.Any;
                          break;
                      case "-?":
                      default:
                          PrintHelpText();
                          flag = true;
                          break;
                  }
              }
  
              // Exit application after help text is displayed
              if (flag)
                  return;
  
  
  
              // Create UDP client
              UdpClient client = new UdpClient(localPort);
              UdpState state = new UdpState(client, remoteSender);
              // Start async receiving
              client.BeginReceive(new AsyncCallback(DataReceived), state);
  
              // Wait for any key to terminate application
              Console.ReadKey();
              client.Close();
          }
  
  
          private static void DataReceived(IAsyncResult ar)
          {
              UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
              IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
              IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
              Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);
  
              bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address)) || wantedIpEndPoint.Address.Equals(IPAddress.Any);
              bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port) || wantedIpEndPoint.Port == 0;
              if (isRightHost &amp;&amp; isRightPort)
              {        string test5 = Encoding.ASCII.GetString(receiveBytes);
  
                  Console.WriteLine("test5 = {0}", test5);
  
  
  
  
              }
  
              // Restart listening for udp data packages
              c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);
  
          }
  
  
          private static string GetValue(string[] args, ref int i)
          {
              string value = String.Empty;
              if (args.Length &gt;= i + 2)
              {
                  i++;
                  value = args[i];
              }
              return value;
          }
  
  
  
          private static void PrintHelpText()
          {
              Console.WriteLine("Simple Udp Receiver is an application that prints incoming data to screen.");
              Console.WriteLine("Data is converted to ASCII before printing.");[![enter image description here][1]][1]           
              Console.WriteLine("Command switches:");
              Console.WriteLine("-? : Displays this text.");
              Console.WriteLine("-lp : Set local receiving port. \"-lp 4001\" Default: 11000");
              Console.WriteLine("-rp : Set remote sender port. \"-rp 4001\" Default: 0 (Any port)");
              Console.WriteLine("-rh : Set remote sender ip. \"-rh 192.168.1.10\" Default: 0 (Any ip)");
              Console.WriteLine("\n Example of usage:\nSimpleUdpReciver.exe -lp 11000 -rh 192.168.10.10 -rp 4001");
          }
  
      }
  }
  </code></pre>
  
  <p>Out put Looks like this whiole running this program : $@ za� �X
  ��   �</p>