Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-Platform Networking Code in C++?

I'm starting development on a new Application, and although my background is mainly Mac/iOS based, I need to work on a Windows application for participation in their Imagine cup.

This project includes communication between clients through a socket connection (to a server, not ad-hoc), and I need for both Mac and Windows clients to be able to communicate with each other. I'd also like to not have to write this networking code twice, and simply write different native-UI code on both platforms. This makes the networking easier (I'm confident that two different platforms are not going to be interacting with the server in different ways) and allows for a native UI on both platforms.

Is C++ the best language for this task? Is the standard library the same on both platforms? I understand that I'll have to use Microsoft's Visual C++ library, as it seems as though it is hard to utilize C++ code from C#; is this true?

I've never really written a cross-platform application before, especially one that deals with networking between platforms.

like image 831
Matt Egan Avatar asked Sep 11 '26 00:09

Matt Egan


2 Answers

If you're going to use C++, I second "the_mandrill" above by strongly recommending you look at ASIO in Boost - that is a great way to write the code once and support both platforms.

As to whether or not C++ is the right language is rather more subjective. My personal feelings are that if you need to ask, odds on, it's not the best approach.

Reasons for selecting C++ to implement networking code are:

  • Possible to achieve very low latencies and high throughput with very carefully designed and implemented code.
  • Possible to take explicit control of memory management - avoiding variations in performance associated with garbage collection.
  • Potential for tight integration of networking code in-process with other native libraries.
  • Potential to build small components suitable for deployment in resource constrained environments.
  • Low level access to C socket API exposes features such as socket options to use protocols beyond vanilla TCP/IP and UDP.

Reasons for avoiding C++ are:

  • Lower productivity in developing code compared with higher-level languages (e.g. Java/C#/Python etc. etc.)
  • Greater potential for significant implementation errors (pointer-abuse etc.)
  • Additional effort required to verify code compiles on each platform.

Alternatives you might consider include:

  • Python... directly using low level socket support or high-level Twisted library. Rapid development using convenient high-level idioms and minimal porting effort. Biggest disadvantage - poor support to exploit multiple processor cores for high-throughput systems.
  • Ruby(socket)/Perl(IO::Socket)... High level languages which might be particularly suited if the communicated information is represented as text strings.
  • Java... garbage collection simplifies memory management; wide range of existing libraries.
  • CLR languages (C# etc.) also garbage collected - like Java... WCF is an effective framework within which bespoke protocols can be developed... which may prove useful.

You also asked: "I understand that I'll have to use Microsoft's Visual C++ library, as it seems as though it is hard to utilize C++ code from C#; is this true?"

You can avoid Visual C++ libraries entirely - either by developing using a language other than C++, or using an alternate C++ compiler - for example Cygwin or MinGW offer G++... though I'd recommend using Visual C++ to build C and C++ code for Windows.

It's not hard to use C++ code from C# - though I don't recommend it as an approach.. it's likely overly complicated. Visual C++ can (optionally) compile "Managed Code" from C++ source (there are a few syntax extensions to grasp and there's a slightly different syntax for interoperation using Mono rather than Visual C++, but these are not major issues IMHO.) These CLR objects interact directly with C# objects and can be linked together into a single assembly without issue. It's also easy to access native DLLs (potentially written using C++ for the native architecture) using Pinvoke. All this is somewhat irrelevant, however, as the .Net framework has good support for low level networking (similar to that provided by Winsock[2]) in system.net - this provides a convenient C#-oriented interface to similar facilities, and will likely provide a more convenient API to develop against if using C# (or VB.Net or any of the other CLR languages.)

like image 149
aSteve Avatar answered Sep 12 '26 13:09

aSteve


I would suggest that you take a look at Qt. IMO Qt is one of the best C++ libraries for doing cross-platform application. The benefits of Qt when comparing with Boost is that Qt have even GUI classes.

like image 35
Lucian Avatar answered Sep 12 '26 14:09

Lucian



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!