I have also seen a couple of questions, recently, on Stack Overflow asking about why networking code wasn't working. Unfortunately I didn't have time to answer them, but it did make me think that there must be a dearth of good samples of networking code online.
Allow me to make that dearth one sample fewer! (Does that make sense?)
For the full listing visit my Github repository: https://github.com/Mellen/Networking-Samples
One problem, that sparked my interest, was how to to keep the server running when a client disconnects. Because the server needs to know when a client disconnects, and not just choke and die. A client disconnecting is not an exceptional circumstance.
The first problem is to not let the server die when a client disconnects, the second is to keep the server looking for new connections, so that it can be a server.
Keep it alive!
My solution to the disconnection problem got generalised to both the client and the server classes, because it makes sense to not have the client die if the server disappears. The user might want to try to reconnect.
You'll find this code in the file NetworkSampleLibrary/NetworkStreamHandler.cs
protected void ReadFromStream(object worker, DoWorkEventArgs args) { BackgroundWorker streamWorker = worker as BackgroundWorker; NetworkStream stream = args.Argument as NetworkStream; try { HandleStreamInput(stream); } catch (Exception ex) { if (ex is IOException || ex is ObjectDisposedException || ex is InvalidOperationException) { streamWorker.CancelAsync(); } if (ex is IOException || ex is InvalidOperationException) { stream.Dispose(); } if (StreamError != null) { StreamError(ex, stream); } } }
You might have noticed that the method is an event handler. More on that below.
As you can see, there are three types of exception that can happen if a client disconnects from the server: IOException, ObjectDisposedException and InvalidOperationException. I found this out through trial and error.
The most common exception that gets thrown when a client disconnects is IOException. This is because the server will be trying to read from the client when it leaves.
Because of the threaded nature of the system, ObjectDisposedExceptions gets thrown when another exception gets thrown and the server still tries to read from the stream in the mean time.
I'm not entirely sure why InvalidOperationException gets thrown, and it doesn't happen a lot, but it is always when the client disconnects.
My strategy is to catch all exceptions, deal with the disconnection exceptions by disposing of the stream if necessary and cancelling the process that reads from the stream, then raising an event that contains the exception and the stream that threw it. I could create a custom exception here, but I settled on an event just in case something that wouldn't catch an exception wanted to know about it.
All are welcome
The next part of the puzzle is to make sure that more than one client can connect to your server.
This is achieved in the NetworkServer class. This can be found at NetworkServerSample / NetworkServer.cs
The pertinent parts are listed below:
public NetworkServer(int port) { _listener = new TcpListener(IPAddress.Any, port); _listener.Start(); _listener.BeginAcceptTcpClient(AcceptAClient, _listener); DataAvilable += SendDataToAll; StreamError += (ex, stream) => { if (ex is IOException || ex is InvalidOperationException || ex is ObjectDisposedException) { _streams.Remove(stream); Console.WriteLine("lost connection {0}", ex.GetType().Name); } else { throw ex; } }; } private void AcceptAClient(IAsyncResult asyncResult) { TcpListener listener = asyncResult.AsyncState as TcpListener; try { TcpClient client = listener.EndAcceptTcpClient(asyncResult); Console.WriteLine("Got a connection from {0}.", client.Client.RemoteEndPoint); HandleNewStream(client.GetStream()); } catch (ObjectDisposedException) { Console.WriteLine("Server has shutdown."); } if (!_disposed) { listener.BeginAcceptTcpClient(AcceptAClient, listener); } } private void HandleNewStream(NetworkStream networkStream) { _streams.Add(networkStream); BackgroundWorker streamWorker = new BackgroundWorker(); streamWorker.WorkerSupportsCancellation = true; streamWorker.DoWork += ReadFromStream; streamWorker.RunWorkerCompleted += (s, a) => { if (_streams.Contains(networkStream) && !a.Cancelled) { streamWorker.RunWorkerAsync(networkStream); } }; streamWorker.RunWorkerAsync(networkStream); }
In the constructor, the server is set up to listen on a particular port for incoming connections and handle the connection requests asynchronously. It also creates an event handler for when the network stream throws an exception, as explained above. This makes sure that the stream is removed from the list of streams, so that it doesn't try to get disposed of when the server is disposed, and that no data gets broadcast down it.
The method that deals with the asynchronous requests for connection (AcceptAClient) has to make sure that the server hasn't been disposed of when the connection attempt is made, hence the try-catch block. Once the connection request has been handled then the method starts listening for another connection attempt. This is all it takes, essentially asynchronous recursion.
The HandleNewStream method also uses asynchronous recursion to read each message from the client. It sets up a BackgroundWorker instance that asynchronously calls the ReadFromStream method in the previous section, and when the work is complete, the worker will call the method again, so long as the stream is in the list of streams on the server and the worker has not been cancelled.
That's the meat of the server. Accepting and handling input from more than one client is achieved with a list and asynchronous recursion. Dealing with clients disconnecting is done with exception handling and events.