title: TCP client to server corrupts data
body: <p>This is very weird.</p>
  
  <p>When I send data to a TCP server, with a TCP client. It corrupts the data, for some extremely odd, and quite annoying reason.</p>
  
  <p>Here is the server code:</p>
  
  <pre><code>TcpListener lis = new TcpListener(IPAddress.Any, 9380); // it needs to be 9380, crucial
  lis.Start();
  Socket sk = lis.AcceptSocket();
  byte[] pd = new byte[sk.ReceiveBufferSize];
  sk.Receive(pd);
  
  // cd is the current directory, filename is the file, ex picture.png, 
  // that was previously sent to the server with UDP.
  File.WriteAllBytes(Path.Combine(cd, filename), pd); 
  </code></pre>
  
  <p>Here is the client code:</p>
  
  <pre><code>// disregard "filename" var, it was previously assigned in earlier code
  byte[] p = File.ReadAllBytes(filename) 
  TcpClient client = new TcpClient();
  client.Connect(IPAddress.Parse(getIP()), 9380);
  Stream st = client.GetStream();
  st.Write(p, 0, p.Length);
  </code></pre>
  
  <p>What happens is data loss. Extreme, sometimes I would upload a 5KB file, but when the server receives and writes the file I send it, it would turn out crazy like 2 bytes. Or it would be 8KB instead! Applications send through this won't even run, pictures show errors, ect.</p>
  
  <p>I would like to note, however, with this, client -> server fails, on the other hand, server -> client works. Strange. </p>
  
  <p>Btw, in case you are interested this is for sending files... Also, using .NET 4.5. Also, my network is extremely reliable.</p>