Sockets

How can I handle over 1000 live sockets
each client socket is almost always reading / writing few bytes
is it really practical to create a thread for each socket
what is the best wat do handle this kind of a problem
thanks,
            Shimi.


Answer this question

Sockets

  • Svyatko

    Creating a thread for each of these 1000 sockets isn't a good design.  You'll need to do Asynchronous communication with these sockets:  http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconusingnon-blockingserversocket.asp is a good article on starting with Asynchronous socket communications.
  • Nick Miller

    Well so managing 1000 sockets in async in one thread is still not the best idea
    maybe to break it down to few threads which will each pickup async reads of the sockets

  • lep31

    Async Delgates arnt runing as threads


  • hamdi4u

    Async sockets aren't like Threads.  Whenever data arrives or there's a need to process, a thread from the threadpool will be used to perform the required action and once that is done, will be returned to the threadpool.

    And it is designed for exactly the same problem you're trying to solve.

  • Daniel Marley

    Do Async and don't worry about threads.  When a work item gets added to the queue, a thread from the thread pool will automatically be picked and given the work.  You don't have to manage threads - it's done by the system.

    The url I pointed to explains this clearly.


  • Beethal

    Async Delegates run in a thread's context (well for that matter anything that ever runs is on a thread).  However, that thread is picked from a pool of threads - and all the thread management is taken care for you - so, you don't have to worry about it. 

    So, what's different is, a new thread is not created everytime your delegate is run.  The same thread will be reused when it's done with the current job.


  • bafidi

    hmm async socket isnt "like" threads
    anyway got up with anotehr idea..
    creating 10 threads which each will handle up to 100 sockets for example
    and handling them using select method to check for incoming data
    the problem is that if i found incoming data how can i proccess it and continue the select method without interaption

  • Sockets