Reputation: 2664
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
select()
call, non-blocking I/O.
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.
Upvotes: 1
Views: 1646
Reputation: 4411
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:
accept
and put the new socket
in a Worker thread.Take a look at the Kegel's c10k page if you want more informations.
Upvotes: 1
Reputation: 24905
Some High Level Ideas from my side. : )
Hope this helps
Upvotes: 2
Reputation: 11063
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/hadouken/hdkn
Upvotes: 0