views:

3165

answers:

3

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.

+8  A: 

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.

Pascal Paradis
This looks to be very helpful. Thanks! Looks like I'll be spending some time reading today.
James Thigpen
+3  A: 

Take a look at this previous question:

http://stackoverflow.com/questions/32198/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.

Eric Z Beard
A: 

you can find a simple multi threaded program from following link.

http://csharp.net-informations.com/communications/csharp-multi-threaded-socket.htm

bolt.

bolton