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

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

我还需要能够干净地关闭监听线程,这是我在网上找不到很多示例的.

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.

我假设 lock/AutoResetEvents/threading 魔术与 async BeginAceptTcpClient 和 EndAcceptTcpClient 的结合可以让我到达那里,但是当涉及到网络代码时,对我来说一切都已经完成了.所以我必须相信我可以遵循一些模式,而不会被无数多线程的极端情况完全混淆,我似乎永远无法做到完美.

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:带模式的多线程服务器.虽然它是 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#.

虽然您可以在 主页.

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

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