user482594
user482594

Reputation: 17486

How should I implement peer-to-peer networking in C++?

I have previously implemented server/client networking programs in C, but I have never done peer-to-peer program or any socket programming in C++.

For peer-to-peer, I guess I would have to create multiple threads and manage incoming and outgoing connections, since each program will work like a client and a server at the same time, right?

What would be a good way to implement this in C++? I believe C++ does not natively support threading...

Upvotes: 0

Views: 6033

Answers (3)

Jeremy Friesner
Jeremy Friesner

Reputation: 73061

You're not required to use multiple threads. An alternative is to use a single thread, and multiplex the sockets using select() (or poll() or epoll() or etc).

Upvotes: 1

Jan Hudec
Jan Hudec

Reputation: 76256

Any code that would be valid in C is also valid in C++. So you can use the same socket API there and the same threading API (but the Boost wrappers might indeed be much more convenient).

Also C++ will natively support threading sometime towards the end of this year (the standard is already written and expected to be voted on by ISO later this year). Of course it may take some time to show up in your compiler/stdlibc++ (but gcc/gnu stdlibc++ already implements it except for language support for thread-local storage (which it does support, but using the older compiler-specific way)). For compilers that don't support it, the boost version is mostly compatible to the proposed standard anyway.

Upvotes: 0

jcoder
jcoder

Reputation: 30035

You might want to look into the boost.asio library which is good for multiple socket connctions (either threaded or not...)

Upvotes: 1

Related Questions