C++ Cross platform Network programming

Started by
12 comments, last by Saruman 15 years, 3 months ago
Hi, if I want to write cross platform networking code in C++ what library should I use? Cheers
Reject the basic asumption of civialisation especially the importance of material possessions
Advertisement
Quote:Original post by Cacks
Hi,

if I want to write cross platform networking code in C++ what library should I use?

Cheers
Plain BSD sockets. On Windows you'll still need to call WSAStartup and WSACleanup, but that's just two function calls.

And the include files are different (sys/socket.h vs winsock2.h)
So I can use the same code, just change the headers & add in the funstion calls?
Reject the basic asumption of civialisation especially the importance of material possessions
Quote:Original post by Cacks
So I can use the same code, just change the headers & add in the funstion calls?
Yup, exactly. If possible, I'd test on a Linux machine, since Windows is a bit more forgiving with some of the function parameters (E.g. the first parameter to select() is unused on windows), and if you're using a fd_set, make sure you use the macros instead of setting struct values directly (Since fd_set is different on Linux).
Cheers Steve
Reject the basic asumption of civialisation especially the importance of material possessions
You can also look at Boost ASIO for cross platform networking.
What about the different underlaying formats of primitives between the different platforms?

Would have to write code to handle this?

If so would it be difficult?

I would like to have it that Windows players could play against Mac players with a direct connection between them,

cheers
Reject the basic asumption of civialisation especially the importance of material possessions
Berkeley sockets and Winsock both provide the htons() and htonl() functions/macros to go between "host" byte order and "network" byte order, and back again. On the PowerPC Macs, those are no-ops. On Windows and x86 Macs, those do swap the byte order.
You can also use a serialization library -- many serialization libraries have a feature where they can serialize on one platform, but de-serialize on another.
enum Bool { True, False, FileNotFound };
Quote:Original post by Evil Steve
Plain BSD sockets. On Windows you'll still need to call WSAStartup and WSACleanup, but that's just two function calls.
[...]
So I can use the same code, just change the headers & add in the funstion calls?
[...]
Yup, exactly.
Almost. You need to make a typedef for the socket and a typedef for a "raw pointer", since they're different on Windows and Linux. Otherwise your compiler will choke on some functions.
You'll need to define the raw pointer type as char* for Windows and void* for Linux, and the socket type as SOCKET for Windows and int for Linux.
Quote:Original post by samoth
You'll need to define the raw pointer type as char* for Windows and void* for Linux, and the socket type as SOCKET for Windows and int for Linux.

There are other, trickier differemces. shutdown() works a little differently, for example, and O_NONBLOCK has vaguely different semantics, and so and so forth. You;re likely never to run into the corner cases doing basic simple stuff.

The most important difference between Windows socket programming and POSIX socket programming is that on a POSIX system, a socket is just a file handle and you can use select() on any file handle. On Windows, sockets live in their own namespace and select() will only work on sockets. Other than that and the WSAStartup requirement, you're good to go.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement