简体   繁体   English

如何随时使用带有并发连接的c#编写TCP Server

[英]How to write a TCP Server, using c# with concurrent connections at any time

We have a fleet of more than 50,000 vehicles in the country and we are going to track our vehicles using gps devices (not mobile devices) fixed in the vehicles, the location will be received by the tcp server, by specifying ip and port of the server. 我们在该国拥有超过50,000辆车的车队,我们将使用固定在车辆中的gps设备(而非移动设备)来跟踪我们的车辆,tcp服务器会通过指定IP地址和端口来接收该位置服务器。

One of the main requirements is concurrent connections, every vehicle will update its location every minute, so can somebody experienced in writing a TCP server, based on 50,000 vehicles, tell me how much concurrent connections I can receive (just an idea) at any time. 主要要求之一是并发连接,每辆车每分钟都会更新其位置,因此编写过基于50,000辆车的TCP服务器的经验丰富的人可以告诉我随时可以接收多少个并发连接(只是一个想法) 。 How can I test it , I mean how can I load test my tcp server. 我如何测试它,我的意思是如何对我的tcp服务器进行负载测试。

If you are aware of any .net library which can be helpful then please guide, I have seen IPDaemon from www.nsoftware.com, and they are claiming 如果您知道任何有用的.net库,请进行指导,我从www.nsoftware.com看过IPDaemon,他们声称

By default, each instance of IPDaemon can handle up to 1,000 simultaneous incoming connections (this number may be increased up to 100,000 or decreased to a lower value by using the MaxConnections configuration setting). 默认情况下,每个IPDaemon实例最多可以处理1,000个并发传入连接(通过使用MaxConnections配置设置,此数目可以增加到100,000,也可以减少到一个较低的值)。

So I am confused and appreciate experienced fellow advice on this. 因此,我感到困惑并感谢经验丰富的同事的建议。

Thanks 谢谢

If the only thing you need to send is a position and some metadata, go with a simple HTTP server. 如果您唯一需要发送的是职位和一些元数据,请使用简单的HTTP服务器。 There are plenty of them in the market, and most are really efficient: managing 50 000 clients should not be a problem, it's not such a high number. 市场上有很多这样的公司,而且大多数都非常有效率:管理5万个客户应该不是问题,这不是一个很高的数目。

I recommend you do not reinvent the wheel or use anything too complicated. 我建议您不要重新发明轮子或使用任何过于复杂的东西。 A simple ASP.NET MVC application should do the trick. 一个简单的ASP.NET MVC应用程序应该可以解决问题。

Avoid keeping a thread per connection, and instead use the Asynchronous Programming Model . 避免为每个连接保留一个线程,而应使用“ 异步编程模型” That means using methods like BeginConnect() , BeginRead() and BeginWrite() along with their corresponding EndXXX methods, instead of the blocking alternatives. 这意味着使用像BeginConnect()BeginRead()BeginWrite()以及它们相应的EndXXX方法,而不是使用阻塞替代方法。

This way, only a handful of thread pool threads will be used internally by the application, so it can scale much better. 这样,应用程序仅在内部使用少量线程池线程,因此可以更好地扩展。 You might have 50,000 calls to BeginRead() , but if only 10 of the connections are actually receiving data at the moment, then you will have up to 10 threads handling them (just giving an example). 您可能有50,000次对BeginRead()调用,但是如果此时只有10个连接实际上正在接收数据,那么最多将有10个线程处理它们(仅举一个例子)。

I don't know how far up it can scale... an update per minute doesn't sound particularly heavy, but you will have to test it to see whether it can withstand the load. 我不知道它可以扩展多远……每分钟的更新听起来并不特别繁琐,但是您必须对其进行测试,以查看它是否能够承受负载。

See this or this for examples of how to write asynchronous socket servers in C#. 请参见有关如何编写C#异步套接字服务器实例。

The concurent connections should never be higher then 1500 (99.99%). 并发连接数绝不能高于1500(99.99%)。 There is no real way to load test it other then doing it on the actualy system. 除了在实际系统上进行测试外,没有真正的方法来对其进行负载测试。 Its a math problem and you can solve it as such. 这是一个数学问题,您可以这样解决。

You have 50000 cars that send a signal every 60 seconds that lasts 1 second(probably less, which will result in even less concurent connections). 您有50000辆汽车每60秒发送一次信号,持续1秒(可能更少,这将导致更少的并发连接)。 Since the sending of the signal isnt synced (the cars arent sending the signal all at the same time, i assume) so they will be more or less arranged like so that you will get 833 connections per second which is 50k/60. 由于信号的发送是不同步的(我想是汽车在同一时间都发送信号),所以它们或多或少地排列成这样,这样您每秒将获得833个连接,即50k / 60。 Now lets say you get "unlucky" and 2000 cars get turned on on the same time (or exactly 60 seconds apart) then you will get 2000 concurent connections for the time those cars are turned on which is extremly unlikely. 现在,让我们假设您“不幸”,同时打开了2000辆汽车(或相隔60秒),然后在打开这些汽车时将获得2000个并发连接,这极不可能。 As i said you can take a 1500 concurent connections as your max but to be really save increase it to 3000 or 4000 which should never happen. 正如我说的那样,您可以将1500个并发连接作为最大连接数,但要真正节省下来,请将其增加到3000或4000,这是永远不会发生的。

This is all considering that the computer will take 1 second to resolve one signal and in reality this time should be closer to 0.1 sec or even lower which would result in 10 times less concurent connections so my real guess of absolute max concurent connections to be around 500. 所有这些都考虑到计算机将花费1秒来解析一个信号,实际上这一次应该接近0.1秒甚至更低,这将导致并发连接减少10倍,因此我对绝对最大并发连接的真实猜测约为500

If .Net is not a hard requirement, i suggest you look in to NodeJs It is pretty lightweight and is getting pretty developed. 如果不是.Net的硬性要求,建议您使用NodeJs。它非常轻巧,并且已经相当成熟。

And as Falanwe said, 50.000 connections per minute is not that much if it is only Location data they are sending. 正如Falanwe所说,每分钟50.000个连接并不是很多,如果仅仅是它们正在发送的位置数据。 This is basicly limited by your bandwidth. 这基本上受带宽限制。 Not much the service on the computer. 计算机上没有太多服务。

I think your biggest challenge is not the TCP connections to the GPS trackers but the database handling. 我认为您最大的挑战不是与GPS跟踪器的TCP连接,而是数据库处理。 The amount of data you will have when you save all the location data for 1 year is massive. 保存所有位置数据1年后,您将拥有的数据量很大。 And the performance of the database server will be very important. 数据库服务器的性能将非常重要。

Also: how will you manage, and what will you do with al the data you collect? 另外:您将如何管理,以及如何对收集到的数据进行处理? You will need to create smart notifications and data filters to extract the data you need for managing your fleet. 您将需要创建智能通知和数据过滤器,以提取管理车队所需的数据。

I have a lot of experience with setting up GPS tracking servers & managing fleets. 我在设置GPS跟踪服务器和管理车队方面有很多经验。 If you have questions feel free to contact me. 如果您有任何疑问,请随时与我联系。

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

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