Question

Are there any templates/patterns/guides I can follow for designing a multithreaded server? I can't find anything terribly useful online through my google searches.

My program will start a thread to listen for connections using TcpListener. Every client connection will be handled by it's own IClientHandler thread. The server will wrap the clientHandler.HandleClient in a delegate, call BeginInvoke, and then quit caring about it.

I also need to be able to cleanly shutdown the listening thread, which is something I'm not finding a lot of exampes of online.

I'm assuming some mix of lock/AutoResetEvents/threading magic combined with the async BeginAceptTcpClient and EndAcceptTcpClient will get me there, but when it comes to networking code, to me it's all been done. So I have to believe there's just some pattern out there I can follow and not get totally confused by the myriad multithreaded corner cases I can never seem to get perfect.

Thanks.

Was it helpful?

Solution

Oddly enough you may find something on a Computer Science Assignment, CSC 512 Programming Assignment 4: Multi-Threaded Server With Patterns. Altough it's C++ voodoo but the theory is quite understandable for someone who can do C#.

Altough you can get the whole list of nice readings on the main page.

OTHER TIPS

Take a look at this previous question:

How do you minimize the number of threads used in a tcp server application?

It's not strictly C# specific, but it has some good advice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top