I am in the design phase of writing a new Windows service application that accepts TCP/IP connections for long running connections (i.e., this is not like HTTP where there are many short connections, but rather a client connects and stays connected for hours or days or even weeks).
I'm looking for ideas for the best way to design the network architecture. I'm going to need to start at least one thread for the service. I am considering using the Asynch API (BeginRecieve, etc.) since I don't know how many clients I will have connected at any given time (possibly hundreds). I definitely do not want to start a thread for each connection.
Data will primarily flow out to the clients from my server, but there will be some commands sent from the clients on occasion. This is primarily a monitoring application in which my server sends status data periodically to the clients.
What is the best way to make this as scalable as possible? Basic workflow?
To be clear, I'm looking for .NET-based solutions (C# if possible, but any .NET language will work).
I would need a working example of a solution, either as a pointer to something I could download or a short example in-line. And it must be .NET and Windows based (any .NET language is acceptable).
Best Solution
I've written something similar to this in the past. From my research years ago showed that writing your own socket implementation was the best bet, using the asynchronous sockets. This meant that clients not really doing anything actually required relatively few resources. Anything that does occur is handled by the .NET thread pool.
I wrote it as a class that manages all connections for the servers.
I simply used a list to hold all the client connections, but if you need faster lookups for larger lists, you can write it however you want.
Also you need the socket actually listening for incoming connections.
The start method actually starts the server socket and begins listening for any incoming connections.
I'd just like to note the exception handling code looks bad, but the reason for it is I had exception suppression code in there so that any exceptions would be suppressed and return
false
if a configuration option was set, but I wanted to remove it for brevity sake.The _serverSocket.BeginAccept(new AsyncCallback(acceptCallback)), _serverSocket) above essentially sets our server socket to call the acceptCallback method whenever a user connects. This method runs from the .NET threadpool, which automatically handles creating additional worker threads if you have many blocking operations. This should optimally handle any load on the server.
The above code essentially just finished accepting the connection that comes in, queues
BeginReceive
which is a callback that will run when the client sends data, and then queues the nextacceptCallback
which will accept the next client connection that comes in.The
BeginReceive
method call is what tells the socket what to do when it receives data from the client. ForBeginReceive
, you need to give it a byte array, which is where it will copy the data when the client sends data. TheReceiveCallback
method will get called, which is how we handle receiving data.EDIT: In this pattern I forgot to mention that in this area of code:
Generally, in the whatever you want code, I would do reassembly of packets into messages, and then create them as jobs on the thread pool. This way the BeginReceive of the next block from the client isn't delayed while whatever message processing code is running.
The accept callback finishes reading the data socket by calling end receive. This fills the buffer provided in the begin receive function. Once you do whatever you want where I left the comment, we call the next
BeginReceive
method which will run the callback again if the client sends any more data.Now here's the really tricky part: When the client sends data, your receive callback might only be called with part of the message. Reassembly can become very very complicated. I used my own method and created a sort of proprietary protocol to do this. I left it out, but if you request, I can add it in. This handler was actually the most complicated piece of code I had ever written.
The above send method actually uses a synchronous
Send
call. For me that was fine due to the message sizes and the multithreaded nature of my application. If you want to send to every client, you simply need to loop through the _sockets List.The xConnection class you see referenced above is basically a simple wrapper for a socket to include the byte buffer, and in my implementation some extras.
Also for reference here are the
using
s I include since I always get annoyed when they aren't included.I hope that's helpful. It may not be the cleanest code, but it works. There are also some nuances to the code which you should be weary about changing. For one, only have a single
BeginAccept
called at any one time. There used to be a very annoying .NET bug around this, which was years ago so I don't recall the details.Also, in the
ReceiveCallback
code, we process anything received from the socket before we queue the next receive. This means that for a single socket, we're only actually ever inReceiveCallback
once at any point in time, and we don't need to use thread synchronization. However, if you reorder this to call the next receive immediately after pulling the data, which might be a little faster, you will need to make sure you properly synchronize the threads.Also, I hacked out a lot of my code, but left the essence of what's happening in place. This should be a good start for you're design. Leave a comment if you have any more questions around this.