在C#中的模式进行多线程网络服务器 [英] Patterns for Multithreaded Network Server in C#

查看:195
本文介绍了在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.

我的程序将启动一个线程来监听使用的TcpListener连接。
每一个客户端连接会被它自己的IClientHandler线程来处理。服务器将包裹clientHandler.HandleClient在一个委托,调用BeginInvoke,然后退出关心它。

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.

我还需要能够正常关闭监听线程,这是一件好事,我没有找到很多网上的exampes的。

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.

我假设锁/ AutoResetEvents /线程魔术异步BeginAceptTcpClient和EndAcceptTcpClient将让我有一些相结合的混合,但是当涉及到​​网络code,对我来说,一切都已经完成。所以,我必须相信只是一些模式在那里我可以跟踪并没有得到完全由无数多线程的角落情况下,我可以似乎从来没有得到完美的混淆。

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.

感谢。

推荐答案

奇怪的是你可能会发现在计算机科学分配的东西,的 CSC 512编程作业4:多线程服务器有图案 。 Altough这是C ++的巫术但理论是完全可以理解的人谁可以做C#。

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你可以在主页不错读数的整个列表。

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

这篇关于在C#中的模式进行多线程网络服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆