Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Patterns for Multithreaded Network Server in C#

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.

like image 841
James Thigpen Avatar asked Sep 29 '08 15:09

James Thigpen


2 Answers

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#.

  • Acceptor/ Connector
  • Monitor Object
  • Thread Safe Interface
  • Wrapper Facade
  • Scoped Locking
  • Strategized Locking
  • Reactor
  • Half Sync/Half-Async
  • Leaders/Followers

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

like image 56
Pascal Paradis Avatar answered Sep 20 '22 17:09

Pascal Paradis


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.

like image 27
Eric Z Beard Avatar answered Sep 18 '22 17:09

Eric Z Beard