写在C#5的异步/的await模式的高度可扩展的TCP / IP的服务器? [英] Writing a highly scalable TCP/IP server in C# 5 with the async/await pattern?
问题描述
我任务是设计必须接受来自多个客户端的连接一个相当简单的TCP / IP服务器。它需要在C#中写的,我使用.NET 4.5。这就是说,我不知道什么是在.NET 4.5的TCP / IP服务器/客户端的可扩展性,目前最先进的技术。
我没有看到这个帖子:如何编写一个可伸缩基于TCP / IP的服务器。但是,这涉及到.NET 2.0和3.5,而没有提到的异步电动机/等待模式。
我能写一个服务器的老办法的......但我想知道什么是新路的说法。
- 什么是使用的插座,TcpClient的或的TcpListener新的异步方法来创建在C#中的可伸缩服务器的最佳方式?
- 请新的异步方法,利用I / O完成端口?
- 是滚动自己的Socket监听器更有效,或者是的TcpListener / TcpClient的类pretty好了吧?
编辑:其他问题
什么是使用的插座,TcpClient的或的TcpListener新的异步方法在C#创建一个可伸缩的服务器的最佳方法是什么?
块引用>有没有
插座
任何新的异步
办法;名为*异步
在插座
的方法是一个特殊的API集,以减少内存使用。的TcpClient
和的TcpListener
确实得到了一些新的异步
方法。如果你想要最好的可扩展性,你可能最好使用斯蒂芬Toub的为
插座
定制awaiters。如果你想最简单的code,你最好使用的TcpClient
和的TcpListener
关闭。
执行新的异步方法,利用I / O完成端口?
块引用>是的,就像大多数的BCL的其他异步的API。据我所知,在
流
类是有可能不使用IOCP唯一的一个;所有其他*开始
/*结束
/*异步
方法使用IOCP。
时的滚动自己的Socket监听器更有效,或者是的TcpListener / TcpClient的类pretty好了吧?
块引用>中的类pretty好,因为它们。斯蒂芬Toub有博客文章即来讲多一点效率的内存使用。
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.
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.
- What is the best way to use the new Async methods on Socket, TcpClient or TcpListener to create a scalable server in C#?
- Do the new Async methods leverage I/O Completion Ports?
- Is rolling your own Socket listener more efficient, or are the TcpListener/TcpClient classes pretty good now?
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#?
There aren't any new
async
methods onSocket
; the methods named*Async
onSocket
are a special set of APIs to reduce memory usage.TcpClient
andTcpListener
did get some newasync
methods.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 usingTcpClient
andTcpListener
.Do the new Async methods leverage I/O Completion Ports?
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.Is rolling your own Socket listener more efficient, or are the TcpListener/TcpClient classes pretty good now?
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的异步/的await模式的高度可扩展的TCP / IP的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!