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

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

问题描述

我是一名经验丰富的 C# 开发人员,但到目前为止我还没有开发过 TCP 服务器应用程序.现在我必须开发一个高度可扩展和高性能的服务器,它可以处理至少 5-1 万个并发连接:通过 GPRS 从 GPS 设备获取原始字节数据.

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 获取新数据,报告,这种情况一再发生
  • 后来 GPS DEVICE 关闭了连接

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

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.

异步套接字方法对我来说似乎是最好的,但以这种异步风格编写代码很糟糕,而且不容易调试.

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

所以我的问题是:您认为在 C# 中实现高性能 TCP 服务器的最佳方式是什么?你知道有什么好的开源组件可以做到这一点吗?(我试了好几个,都没有找到好的.)

So my question 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.)

推荐答案

必须是异步的,没有办法解决.高性能和可扩展性不会与每个套接字一个线程混合在一起.你可以看看 StackExchange 自己在做什么,参见 async Redisawait BookSleeve 它利用了下一个 C# 版本中的 CTP 功能(因此处于边缘并且可能会发生变化,但它很酷).为了获得更多前沿技术,解决方案围绕利用 SocketAsyncEventArgs Class 通过消除与经典"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天全站免登陆