The most efficient way to manage multiple socket(maximum 50 sockets.) in a single process?

StackOverflow https://stackoverflow.com/questions/17446896

  •  02-06-2022
  •  | 
  •  

Question

I'm trying to implement Bittorrent client. in order to receive pieces from different peers, The client should manage multiple socket.

Well-known solution that I know are

  • 1. Each thread has one socket.
  • 2. Using select() call, non-blocking I/O.
  • 3. a mix of 1 and 2.

    The first solution requires too many threads. The second solution wastes CPU time since it continue to checks maximum 50 socket. Also, when deciding to use the third solution, I don't know how many threads a single process use.

    Which solution is the best one, to receive a fairly large file?
    Is there any web page that give me a good solution?

    Any advice would be awesome.

  • Was it helpful?

    Solution

    Some High Level Ideas from my side. : )

    1. Have a main thread in which you will be doing the "select" / "poll" call for all the connections.
    2. Have a thread pool of worker threads
    3. If for a particular connection, select indicates that there is data to read, then pass the socket + additional information to one of the free worker threads for receiving / sending data on that connection.
    4. Upon completion of the work, the worker thread returns to the free worker thread queue, which can be used again for another connection.

    Hope this helps

    OTHER TIPS

    You're right, the first solution is the worst. The second one, with select() can do the job, but there's a problem: select() has a complexity of log(n). You should use /dev/poll, epoll(), kqueue() or whatever, but don't use select().

    Don't use one thread per socket !! You will loose a lot of time due to the context switch.

    You should have:

    • A Listener thread : just do all the accept and put the new socket in a Worker thread.
    • Multiple Worker thread: do all the other stuff. It will check if there's data available and will handle it. A Worker thread manage many sockets.

    Take a look at the Kegel's c10k page if you want more informations.

    Check some Open Source BitTorrent client and check the code to get some ideas, it is the best thing you could do.

    I recommend you to check BitTorrent in C or Hadouken in C# for example:

    https://github.com/bittorrent

    https://github.com/hadouken/hdkn

    Licensed under: CC-BY-SA with attribution
    Not affiliated with StackOverflow
    scroll top