Search code examples
csocketsbittorrent

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


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.


  • 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