简体   繁体   中英

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.

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. How can I test it , I mean how can I load test my tcp server.

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

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).

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. 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.

I recommend you do not reinvent the wheel or use anything too complicated. A simple ASP.NET MVC application should do the trick.

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.

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).

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#.

The concurent connections should never be higher then 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). 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. 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. 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.

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.

If .Net is not a hard requirement, i suggest you look in to NodeJs It is pretty lightweight and is getting pretty developed.

And as Falanwe said, 50.000 connections per minute is not that much if it is only Location data they are sending. 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. The amount of data you will have when you save all the location data for 1 year is massive. 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. If you have questions feel free to contact me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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