Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using readv(), writev() and poll() from C++

There is a multiplayer card game which I had first programmed as a non-forking socket server in C (using poll() call).

Then it was too difficult for me to add new features and I've switched to Perl (using IO::Poll module).

As Perl doesn't support readv()/writev(), requires more memory/CPU and also isn't very commercial (I'd like to sell my game later), I would like to port my server back to C++ in future - once my features stabilize. (C++ this time because there are few objects in my server).

Could anyone please supply me with an example, how to use readv(), writev() and poll() or select() under C++? I know how to use those under Perl and C, but I haven't found any examples for C++ yet.

My environment: I'm using OpenBSD with its gcc/g++ and I'd like my server to run under Linux as well. I'd prefer not to use any unusual libraries (like libevent?) unless they work under Windows too - because maybe in the future I want to port my server to Windows too (for that I think I'll have to switch back from poll() to select() and add few Winsock functions?).

Please let me add that I like both Perl and C and I respect C++ very much, so this question is not about which language or OS is better. My question is: how to use poll() with C++

Thank you! Alex

like image 775
Alexander Farber Avatar asked Dec 11 '25 01:12

Alexander Farber


2 Answers

The functions readv(), writev(), and poll() work the same way in C++ as they do in C.

like image 61
mark4o Avatar answered Dec 13 '25 16:12

mark4o


C++ handles them exactly the same as C -- Here's a tutorial to get you started.

However, since you're writing it in C++, I'd strongly advise you to take a look at the powerful alternatives, like the Boost.Asio framework. Believe me, you'll save a lot of time and nerves in implementing a server using Asio.

like image 39
Kornel Kisielewicz Avatar answered Dec 13 '25 15:12

Kornel Kisielewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!