title: Getting started with socket programming in C# - Best practices
body: <p>I have seen many resources here on SO about Sockets. I believe none of them covered the details which I wanted to know. In my application, server does all the processing and send periodic updates to the clients. </p>
  
  <p>Intention of this post is to cover all the basic ideas required when developing a socket application and discuss the best practices. Here are the basic things that you will see with almost all socket based applications.</p>
  
  <p>1 - <strong>Binding and listening on a socket</strong></p>
  
  <p>I am using the following code. It works well on my machine. Do I need to take care about something else when I deploy this on a real server?</p>
  
  <pre><code>IPHostEntry localHost = Dns.GetHostEntry(Dns.GetHostName());
  IPEndPoint endPoint = new IPEndPoint(localHost.AddressList[0], 4444);
  
  serverSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, 
                       ProtocolType.Tcp);
  serverSocket.Bind(endPoint);
  serverSocket.Listen(10);
  </code></pre>
  
  <p>2 - <strong>Receiving data</strong></p>
  
  <p>I have used a 255 sized byte array. So when I am receiving data which is more than 255 bytes, I need to call the receive method until I get the full data, right? Once I got the full data, I need to append all the bytes received so far to get the  full message. Is that correct? Or is there a better approach?</p>
  
  <p>3 - <strong>Sending data and specifying the data length</strong></p>
  
  <p>Since there is no way in TCP to find the length of the message to receive, I am planning to add the length to the message. This will be the first byte of the packet. So client systems knows how much data is available to read. </p>
  
  <p>Any other better approach?</p>
  
  <p>4 - <strong>Closing the client</strong></p>
  
  <p>When client is closed, it will send a message to server indicating the close. Server will remove the client details from it's client list. Following is the code used at client side to disconnect the socket (messaging part not shown).</p>
  
  <pre><code>client.Shutdown(SocketShutdown.Both);
  client.Close();
  </code></pre>
  
  <p>Any suggestions or problems?</p>
  
  <p>5 - <strong>Closing the server</strong></p>
  
  <p>Server sends message to all clients indicating the shutdown. Each client will disconnect the socket when it receives this message. Clients will send the close message to server and close. Once server receives close message from all the clients, it disconnects the socket and stop listening. Call <em>Dispose</em> on each client sockets to release the resources. Is that the correct approach?</p>
  
  <p>6 - <strong>Unknown client disconnections</strong></p>
  
  <p>Sometimes, a client may disconnect without informing the server. My plan to handle this is: When server sends messages to all clients, check the socket status. If it is not connected, remove that client from the client list and close the socket for that client. </p>
  
  <p>Any help would be great!</p>