-3

I'm writing a P2P program in C++ such that the program is running in multiple hosts and each host is connected directly to each other (i.e. full graph).

My question is about the consideration that I should take into account when dealing with sockets in that case.

I need the communication to not a bottleneck and have really good time performance. Should I use separate thread for each connection? or some kind of infinite loop? what about accepting new connections? (i.e. accept()). Doit make any difference whether it is under linux or Windows? What other matters should I consider?

I found several question deal with a subset of my problem, but they weren't fully addressed my concerns. (this and this for instance)

Community
  • 1
  • 1
Bush
  • 2,433
  • 5
  • 34
  • 57

1 Answers1

0

I would suggest asynchronous networking for such things, boost::asio and QtNetwork being good examples. They use system-dependent features (like epoll, iocp, kqueue and others) that take 0% CPU (while an infinite loop would normally take 100%). The performance is great, provided that the library is used properly.

I advise reading a bit about something like epoll to understand how it works under the hood.

By the way, this approach can use any number of threads (even be single-threaded), so you can configure it for improving performance.

lisyarus
  • 15,025
  • 3
  • 43
  • 68