best way to deal with lots of sockets in C # ?

Answered best way to deal with lots of sockets in C # ?

  • Sunday, August 05, 2012 12:15 AM
     
      Has Code

    hey

    I write a server software, I expect to have many many sokets open simultaneously.

    but I do not know the fastest way to read informaconen from my tcp sockets.

    My current code looks a bit like this : 

    System.Net.Sockets.Socket[] Sockets;
    
    Sockets = new System.Net.Sockets.Socket[10000];
    
    for (int i = 0; i < Sockets.Length; i++)
    {
          if(Sockets[i] != null && Sockets[i].Available != 0)
          {
          byte[] data  = new byte[Sockets[i].Available];
          Sockets[i].Receive(data);
          // manage data ..................
          }
    }

    I do this in many threads, in order to gain performance.

    but is there a better way? or tps, I want high speed in my sockets!

    thanks in advance!!
    • Edited by mad-YuRi Sunday, August 05, 2012 12:16 AM haha not thanks to the hand! : )
    •  

All Replies

  • Sunday, August 05, 2012 2:32 AM
     
     Answered

    Each Socket should be put into a background thread. Then you can do the asychornized work.

    For example u can download here:

    http://www.codeproject.com/Articles/2477/Multi-threaded-Client-Server-Socket-Class

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    • Marked As Answer by mad-YuRi Monday, August 06, 2012 9:17 AM
    •  
  • Sunday, August 05, 2012 3:18 AM
    Moderator
     
     Answered

    Actually using a lot of threads is the wrong way to go.  The problem is that you will swamp the system with too many threads such that there will be more thread swapping than actual work.  For high performance systems, such as web browsers, a thread pool (not the thread pool) is used to manage the connections.  When a request comes in one of the threads from the pool is grabbed to process the request.  When the request is completed the thread is added back to the pool.  Note that a request is generally a single message from the client.  The socket can remain alive indefinitely but each request sent on the socket from the client can potentially go to a different thread. 

    This keeps system resources in control while allowing for potentially unlimited connections to the server.  There is always an upper limit on simultaneous connections but this is generally not a big deal.  In most software it is configurable allowing you to scale up on better hardware.  Creating threads is expensive so most high performance software pull threads from the pool.  If no threads are available and there is room for more then a new thread is created.  Once the pool is maxed all future calls block until a thread can be used.  This prevents the system from eating up all the resources.  Some advanced systems are also smart enought to stop threads if they have been idle for a long time.  This allows the system to better adjust to demand but it requires a lot of extra work.  Note that in this discussion of thread pools I'm not talking about the thread pool provided by .NET.  You should create your own set of threads (your pool) otherwise you will be struggling with the existing usage of the pool for your threads.

    Beyond that socket communication is the fastest option available.  You can't really optimize performance beyond the raw socket speed.  All you can do is make sure your server responds to messages quickly.  Logging of the time spent in a request call will help identify performance issues.

    Michael Taylor - 8/4/2012
    http://msmvps.com/blogs/p3net

    • Marked As Answer by mad-YuRi Monday, August 06, 2012 9:17 AM
    •  
  • Sunday, August 05, 2012 1:51 PM
     
     

    I already use System.Threading.ThreadPool.
    and I used a socket array where that is not used is null. just because array is faster to loop through.

    is "asychornized work" faster than my method?

  • Sunday, August 05, 2012 4:39 PM
     
     Answered
    If you're already using an approach like CoolDadTx outlined, then the next step is to start optimizing your data. Take a look at the data you are sending and see if you can make it smaller so you're not sending as much over the wire.

    Currently developing FaultTrack. I occassionally blog about C# and .NET.

    • Marked As Answer by mad-YuRi Monday, August 06, 2012 9:17 AM
    •  
  • Sunday, August 05, 2012 7:54 PM
     
     Answered

    I already use System.Threading.ThreadPool.
    and I used a socket array where that is not used is null. just because array is faster to loop through.

    is "asychornized work" faster than my method?

    Yes... asyncron routines are fast because it uses IO Completion Ports so you can handle 1000s of connections with a few threads. In .net 4 asycron methods is using IOC Ports you cannot access the thread pool it is internaly implemented by .net framwwork  just use event model of async calls. 

    http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs

    There is an another option , it is the HTTPListener class. It also uses IOCP but on http.sys. But it s lack of Disconnect event ,  you must implement youself.

    • Marked As Answer by mad-YuRi Monday, August 06, 2012 9:17 AM
    •  
  • Monday, August 06, 2012 9:18 AM
     
     
    thanks for all the answers, I feel like a wiser man now !