使用异步/等待模式在 C# 5 中编写高度可扩展的 TCP/IP 服务器? [英] Writing a highly scalable TCP/IP server in C# 5 with the async/await pattern?

查看:17
本文介绍了使用异步/等待模式在 C# 5 中编写高度可扩展的 TCP/IP 服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是设计一个相当简单的 TCP/IP 服务器,它必须接受来自多个客户端的连接.它需要用 C# 编写,我使用的是 .NET 4.5.也就是说,我不确定 .NET 4.5 中 TCP/IP 服务器/客户端可扩展性的当前最新技术"是什么.

I'm tasked with designing a fairly simple TCP/IP server that must accept connections from multiple clients. It needs to be written in C#, and I'm using .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.

我确实看到了这篇文章:如何编写可扩展的基于 Tcp/Ip 的服务器.但这与 .NET 2.0 和 3.5 相关,并没有提及 async/await 模式.

I did see this post: 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.

我能够以旧方式"编写服务器……但我想知道新方式"是什么.

I am capable of writing a server the "old way"... but I want to know what the "new way" is.

  • 在 Socket、TcpClient 或 TcpListener 上使用新的 Async 方法在 C# 中创建可扩展服务器的最佳方法是什么?
  • 新的异步方法是否利用了 I/O 完成端口?
  • 滚动您自己的 Socket 侦听器是否更有效,或者 TcpListener/TcpClient 类现在是否很好?

其他问题.

推荐答案

在 Socket、TcpClient 或 TcpListener 上使用新的 Async 方法在 C# 中创建可扩展服务器的最佳方法是什么?

What is the best way to use the new Async methods on Socket, TcpClient or TcpListener to create a scalable server in C#?

Socket 上没有任何新的 async 方法;Socket 上名为 *Async 的方法是一组特殊的 API,用于减少内存使用.TcpClientTcpListener 确实获得了一些新的 async 方法.

There aren't any new async methods on Socket; the methods named *Async on Socket are a special set of APIs to reduce memory usage. TcpClient and TcpListener did get some new async methods.

如果您想要最佳的可扩展性,您可能最好使用 Stephen Toub 的自定义Socket 的等待器.如果您想要最简单的编码,最好使用 TcpClientTcpListener.

If you want the best scalability, you're probably best using Stephen Toub's custom awaiters for Socket. If you want the easiest to code, you're probably better off using TcpClient and TcpListener.

新的异步方法是否利用 I/O 完成端口?

Do the new Async methods leverage I/O Completion Ports?

是的,就像 BCL 中的大多数其他异步 API 一样.AFAIK,Stream 类是唯一可能不使用 IOCP 的类;所有其他 *Begin/*End/*Async 方法都使用 IOCP.

Yes, just like most of the other asynchronous APIs in the BCL. AFAIK, the Stream class is the only one that may possibly not use the IOCP; all other *Begin/*End/*Async methods use the IOCP.

滚动您自己的 Socket 侦听器是否更有效,或者 TcpListener/TcpClient 类现在是否很好?

Is rolling your own Socket listener more efficient, or are the TcpListener/TcpClient classes pretty good now?

课程非常好.Stephen Toub 有一篇博客文章,在内存使用.

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.

这篇关于使用异步/等待模式在 C# 5 中编写高度可扩展的 TCP/IP 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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