title: c# UDP client server problems
body: <p>I have a udp client and a udp server.
  The udp client and server is really disconnected physically.
  So I can only send messages to the udp server but I can't get acknowledgements whether the data was receieved properly.</p>
  
  <p>So what I am doing is to send a header of 4 bytes that will specify the amount of data I am about to send.
  And then I read the data according to the size I sent earlier.</p>
  
  <p><b>Client Side</b>
  </p>
  
  <pre><code>// Sending Header(header is the size of the data)
  byte[] header = BitConverter.GetBytes(data.Count());
  socket.SendTo(header, 0, header.Count(), SocketFlags.None, outEP);
  // Sending Data
  socket.SendTo(data, 0, data.Count(), SocketFlags.None, outEP);
  </code></pre>
  
  <p><b>Server Side</b>
  </p>
  
  <pre><code>// Receiving input size
  int receivedCount = socket.EndReceive(result);
  // Header is filled on BeginReceive
  Int32 count = BitConverter.ToInt32(header, 0);
  if(count != receivedCount) throw;
  // Then I receive the count relevent
  socket.Receive(data, 0, count, SocketFlags.None);
  </code></pre>
  
  <p>A couple problems come up:</p>
  
  <ol>
  <li><p>count != receivedCount</p>
  
  <p>This is why I did the check, but it sometimes happens and sometimes it doesn't..I do need a maximum guarantee.
  I found out that when the count is different its not because of fallen bytes or network issues. I just sometimes don't receive the header - only the data and when I read the 4 bytes I actually read the 4 bytes of the data thats why it fails.</p>
  
  <p>The data just sometimes <strong>Overrides</strong> the header.
  <em>Why is that? and how can I solve this?</em></p></li>
  <li><p>Sometimes I also get the following error on the socket.Receive</p>
  
  <p><em>A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself</em></p>
  
  <p>The problem is that is sometimes happens and most of the times it doesn't...
  I can just receive it all in a max buffer..but then I would have memory problems.</p>
  
  <p>This should be in high performance and guaranteed delivery as much as possible..
  This must be udp because the server side can't physically contact the client side
  It is one way - client to server.</p></li>
  </ol>