title: TCP C# Client and Server by BinaryWriter/Reader
body: <p>I have to do 2 simple programs that one of them is the server(only receiving) and the other is client(only sending). 
  My trying code : 
  <strong>Server :</strong></p>
  
  <pre><code>// Definition for TCP connection with simulation Radar
  private TcpClient c;
  private NetworkStream n;
  private BinaryReader reader;
  public static TcpListener listenerForRadarLocation;
  
  // Definition for sendRadarLocation message
  private string nameOfRadar;
  private UInt32 typeOfRadar;
  private UInt32 AzimuthStart;
  private UInt32 AzimuthEnd;
  private UInt32 Alt_flag;
  private UInt32 LongRadar;
  private UInt32 LatRadar;
  private UInt32 Zaltitude;
  private UInt32 Range_min;
  private UInt32 Range_max;
  private UInt32 Azimuth;
  private UInt32 HighAzimuth;
  
  //In button received click event :
  listenerForRadarLocation = new TcpListener(new IPAddress(new byte[] { 192, 168, 43, 145 }), 1300);
  listenerForRadarLocation.Start();
  using (c = listenerForRadarLocation.AcceptTcpClient())
  {
      using (n = c.GetStream())
      {
          reader = new BinaryReader(n);
          nameOfRadar = reader.ReadString();
          typeOfRadar = reader.ReadUInt32();
  
          AzimuthStart = reader.ReadUInt32();
          AzimuthEnd = reader.ReadUInt32();
  
          Alt_flag = reader.ReadUInt32();
  
          LongRadar = reader.ReadUInt32();
          LatRadar = reader.ReadUInt32();
  
          Zaltitude = reader.ReadUInt32();
          Range_min = reader.ReadUInt32();
          Range_max = reader.ReadUInt32();
          Azimuth = reader.ReadUInt32();
          HighAzimuth = reader.ReadUInt32();
      }
  }
  </code></pre>
  
  <p><strong>Client :</strong></p>
  
  <pre><code>private TcpClient client;
  private NetworkStream n;
  private BinaryWriter w;
  
  // Definition for sendRadarLocation message
  private string nameOfRadar;
  private UInt32 typeOfRadar;
  private UInt32 Alt_flag;
  private UInt32 Zaltitude;
  private UInt32 Range_min;
  private UInt32 Range_max;
  private UInt32 Azimuth;
  private UInt32 HighAzimuth;
  
  // Initiallized for SendLocation Massege
  nameOfRadar = "00000000";
  typeOfRadar = 7;
  Alt_flag = 1;
  Zaltitude = 555;
  Range_min = 0;
  Range_max = 0;
  Azimuth = 9999;
  HighAzimuth = 0;
  
  
  //In button send click event :
  
  using (client = new TcpClient("192.168.43.145", 1300))
  using (n = client.GetStream())
  {
      w = new BinaryWriter(n);
  
      w.Write(nameOfRadar);
      w.Write(typeOfRadar);
  
      w.Write(AzimuthStart.Text.ToString());
      w.Write(AzimuthEnd.Text.ToString());
  
      w.Write(Alt_flag);
  
      w.Write(LongRadar.Text.ToString());
      w.Write(LatRadar.Text.ToString());
  
      w.Write(Zaltitude);
      w.Write(Range_min);
      w.Write(Range_max);
      w.Write(Azimuth);
      w.Write(HighAzimuth);
  
      w.Flush();
  }
  </code></pre>
  
  <p>The result is:</p>
  
  <ol>
  <li><p><strong>End of stream exaption : Unable to read beyond the end of the stream.</strong>
  As I understand from the error, it reads over than the stream have. But I sending 12 variables, and received 12 variables. </p></li>
  <li><p>Another error is the textbox.Text.ToString() - it send the right string, but in the received it get a long number like : "82645173" or another number like this (even if I send the number 1).
  Thanks.</p></li>
  </ol>