Patterns for Multithreaded Network Server in C#(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# 中多线程网络服务器的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 中多线程网络服务器的模式
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
