在C#中的高性能TCP服务器 [英] High performance TCP server in C#

查看:486
本文介绍了在C#中的高性能TCP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个有经验的C#开发,但我还没有开发出TCP服务器应用程序为止。现在我必须开发能够处理至少一千5-10并发连接一个高度可扩展和高性能服务器:通过GPRS从GPS设备获取-raw字节级数据

I am an experienced C# developer, but I have not developed a TCP server application so far. Now I have to develop a highly scalable and high performance server that can handle at least 5-10 thousand concurrent connections: getting -raw byte- data via GPRS from GPS devices.

有一个共同的沟通过程应该是这样的:

A common communication process should look like this:


  • GPS设备启动到我的服务器
  • 的连接
  • 我的服务器的回答,如果我想获得的数据

  • 设备发送的GPS数据

  • 我的服务器发送报告,设备得到它(SG像校验)

  • 从GPS,reportm获得新的数据,这频频发生

  • 后GPS设备关闭连接

所以,在我的服务器我需要

So, in my server I need


  • 跟踪连接/活动的客户端

  • 来从服务器端关闭任何客户端

  • 捕捉事件,当设备关闭连接

  • 获取字节数据

  • 将数据发送给客户

我开始阅读关于这个话题在互联网上,但它似乎是对我来说是一场噩梦。有很多办法,但我无法找出哪些是最好的。

I started to read about this topic over the internet, but it seems to be a nightmare for me. There are a lot of ways, but I could not find out which is the best.

异步插座方法似乎是最适合我的,但在这种异步文风code是可怕的,不容易调试。

Async socket methods seems the best for me, but writing code in this async style is terrible and not easy to debug.

所以我的quetsion是:你认为哪个实现在C#中的高性能TCP服务器的最佳方式?你知道有什么好的开源组件来做到这一点? (我试了的,但我无法找到一个好。)

So my quetsion is: which do you think the best way to implement a high performance TCP server in C#? Do you know any good open source component to do this? (I tried several ones, but I could not find a good one.)

在此先感谢,任何专家的答案将appriciated!

Thanks in advance, any expert answers would be appriciated!

推荐答案

它必须是异步,有没有办法解决这个。高性能和可扩展性不与一个线程每个插座混用。你可以看看什么StackExchange自己在做什么,请参见 Redis的异步等待BookSleeve 利用的是CTP从下一个C#版功能(所以上的优势,可作改动,但它是凉)。为了更前沿的解决方案的发展借力周围的SocketAsyncEventArgs类,通过消除与经典C#异步处理相关的异步处理频繁的分配进一步采取一步算一步:

It must be async, there is no way around this. High performance and scalability don't mix with one-thread-per-socket. You can have a look at what StackExchange themselves are doing, see async Redis await BookSleeve which leverages the CTP features from the next C# release (so is on the edge and subject to changes, but it is cool). For even more bleeding edge the solutions evolves around leveraging SocketAsyncEventArgs Class which takes things one step further by eliminating the frequent allocations of async handlers associated with 'classic' C# async processing:

该让SocketAsyncEventArgs类是部分
  一套增强到的
  System.Net.Sockets.Socket类
  提供了另一种异步
  可通过使用图案
  专业的高性能插座
  应用程序。这个类是
  专为网络设计
  要求高的服务器应用程序
  性能。应用程序可以使用
  增强的异步模式
  完全或仅在有针对性的热
  区域(例如,在接收时
  大量的数据)。

The SocketAsyncEventArgs class is part of a set of enhancements to the System.Net.Sockets.Socket class that provide an alternative asynchronous pattern that can be used by specialized high-performance socket applications. This class was specifically designed for network server applications that require high performance. An application can use the enhanced asynchronous pattern exclusively or only in targeted hot areas (for example, when receiving large amounts of data).

长话短说:学习异步或死亡的尝试...

Long story short: learn async or die trying...

顺便说一句,如果你问的为什么的异步,那么阅读这篇文章链接的三篇文章:的高性能Windows程序的。最终的答案是:底层操作系统的设计要求它

BTW, if you're asking why async, then read the three articles linked from this post: High Performance Windows programs. The ultimate answer is: the underlying OS design requires it.

这篇关于在C#中的高性能TCP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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