Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage many TCP connections effectively and efficiently

Tags:

c#

.net

tcp

c#-4.0

I am curious if someone can give me any ideas or suggestions on how to manage a lot of TCP connections efficiently. I am talking around 1000 tcp connections (maybe more). The application managing all these connections needs to pull information from the client (other end of the connection) periodically. For example, maybe every 30 seconds. I would be using .NET 4.0 for this. Is there anything built in to help with this or a special way to structure everything to manage all these connections without making this application be soo bogged down that it is not useful?

like image 740
Travyguy9 Avatar asked Dec 13 '22 17:12

Travyguy9


1 Answers

I can give you one quick bit of insight:

Do not even think about spawning a thread per-connection. Use the framework's built-in async/task frameworks instead to manage the connections. That is; let the thread pool do it.

Also, be careful to watch closely what your threads are doing; Be sure you can't ever be reading/writing a connection's stream from multiple threads at once. But at the same time, be careful with how you use locking mechanisms to do so, so you don't end up with a process with 1000 active connections, and a thread pool full of threads waiting to acquire locks that they will never get.

like image 126
Andrew Barber Avatar answered Jan 03 '23 05:01

Andrew Barber