简体   繁体   English

使用异步/等待模式在 C# 5 中编写高度可扩展的 TCP/IP 服务器?

[英]Writing a highly scalable TCP/IP server in C# 5 with the async/await pattern?

I'm tasked with designing a fairly simple TCP/IP server that must accept connections from multiple clients.我的任务是设计一个相当简单的 TCP/IP 服务器,它必须接受来自多个客户端的连接。 It needs to be written in C#, and I'm using .NET 4.5.它需要用 C# 编写,而我使用的是 .NET 4.5。 That said, I'm not sure what is the current "state of the art" for TCP/IP server/client scalability in .NET 4.5.也就是说,我不确定 .NET 4.5 中 TCP/IP 服务器/客户端可伸缩性的当前“最新技术”是什么。

I did see this post: How to write a scalable Tcp/Ip based server .我确实看过这篇文章: How to write a scalable Tcp/Ip based server But that relates to .NET 2.0 and 3.5 and makes no mention of the async/await pattern.但这与 .NET 2.0 和 3.5 相关,并且没有提及异步/等待模式。

I am capable of writing a server the "old way"... but I want to know what the "new way" is.我有能力以“旧方式”编写服务器......但我想知道“新方式”是什么。

  • What is the best way to use the new Async methods on Socket, TcpClient or TcpListener to create a scalable server in C#?在 Socket、TcpClient 或 TcpListener 上使用新的 Async 方法在 C# 中创建可伸缩服务器的最佳方法是什么?
  • Do the new Async methods leverage I/O Completion Ports?新的异步方法是否利用 I/O 完成端口?
  • Is rolling your own Socket listener more efficient, or are the TcpListener/TcpClient classes pretty good now?滚动您自己的 Socket 侦听器是否更有效,或者 TcpListener/TcpClient 类现在非常好?

EDIT: Additional questions.编辑:其他问题。

What is the best way to use the new Async methods on Socket, TcpClient or TcpListener to create a scalable server in C#?在 Socket、TcpClient 或 TcpListener 上使用新的 Async 方法在 C# 中创建可伸缩服务器的最佳方法是什么?

There aren't any new async methods on Socket ; Socket上没有任何新的async方法; the methods named *Async on Socket are a special set of APIs to reduce memory usage. Socket上名为*Async的方法是一组特殊的 API,用于减少内存使用。 TcpClient and TcpListener did get some new async methods. TcpClientTcpListener确实获得了一些新的async方法。

If you want the best scalability, you're probably best using Stephen Toub's custom awaiters for Socket .如果您想要最好的可伸缩性,您可能最好使用Stephen Toub 的Socket自定义等待程序 If you want the easiest to code, you're probably better off using TcpClient and TcpListener .如果您想要最简单的代码,最好使用TcpClientTcpListener

Do the new Async methods leverage I/O Completion Ports?新的异步方法是否利用 I/O 完成端口?

Yes, just like most of the other asynchronous APIs in the BCL.是的,就像 BCL 中的大多数其他异步 API 一样。 AFAIK, the Stream class is the only one that may possibly not use the IOCP; AFAIK, Stream类是唯一可能不使用 IOCP 的类; all other *Begin / *End / *Async methods use the IOCP.所有其他*Begin / *End / *Async方法都使用 IOCP。

Is rolling your own Socket listener more efficient, or are the TcpListener/TcpClient classes pretty good now?滚动您自己的 Socket 侦听器是否更有效,或者 TcpListener/TcpClient 类现在非常好?

The classes are pretty good as they are.这些课程非常好。 Stephen Toub has a blog post that is a bit more efficient in terms of memory use. Stephen Toub 有一篇博客文章,在内存使用方面效率更高一些。

I'm tasked with designing a fairly simple TCP/IP server that must accept connections from multiple clients.我的任务是设计一个相当简单的 TCP/IP 服务器,它必须接受来自多个客户端的连接。 It needs to be written in C#, and I'm using .NET 4.5.它需要用 C# 编写,我使用的是 .NET 4.5。 That said, I'm not sure what is the current "state of the art" for TCP/IP server/client scalability in .NET 4.5.也就是说,我不确定 .NET 4.5 中 TCP/IP 服务器/客户端可扩展性的当前“最新技术”是什么。

I did see this post: How to write a scalable Tcp/Ip based server .我确实看到了这篇文章: 如何编写可扩展的基于 Tcp/Ip 的服务器 But that relates to .NET 2.0 and 3.5 and makes no mention of the async/await pattern.但这与 .NET 2.0 和 3.5 相关,并没有提及 async/await 模式。

I am capable of writing a server the "old way"... but I want to know what the "new way" is.我能够以“旧方式”编写服务器……但我想知道“新方式”是什么。

  • What is the best way to use the new Async methods on Socket, TcpClient or TcpListener to create a scalable server in C#?在 Socket、TcpClient 或 TcpListener 上使用新的 Async 方法在 C# 中创建可扩展服务器的最佳方法是什么?
  • Do the new Async methods leverage I/O Completion Ports?新的异步方法是否利用 I/O 完成端口?
  • Is rolling your own Socket listener more efficient, or are the TcpListener/TcpClient classes pretty good now?滚动您自己的 Socket 侦听器是否更有效,或者 TcpListener/TcpClient 类现在是否很好?

EDIT: Additional questions.编辑:其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM