I'm trying to use C++ sockets to control my rpi car/tank , but I'm a bit confused .

My goal is to have an html interface with a onscreen joystick , and use javascript to send X and Y coordinates trough socket . I googled , but there are too much libraries to use sockets , and I'm confused . can you suggest one ?

  • This is a very broad question--fortunately goldilocks left a good answer, but in the future please keep questions specific to Raspberry Pi. – ramblinjan Oct 21 '13 at 20:04

Sockets aren't part of standard C (or C++) because they are platform specific. Since you're almost certainly using linux on the pi, you want to use the glibc implementation. That's a C reference, as GNU's C++ library does not include any additional wrappers; the normal approach would be to write your own. There's an example of such here.

Boost has a lot of popular cross-platform C++ libraries for various things, and is available on raspbian (apt-cache search boost | grep C++ will show you the packages). This includes boost.asio (asio = asynchronous input/output) which covers sockets. I haven't used it so can't vouch for it directly, but it no doubt works and they have lots of docs.

My personal recommendation would be to try and do it without a library; most of the tutorials you'll find (remember: for linux) will be C oriented but working with the native C facilities is an important skill for a C++ programmer -- otherwise you are dependent on other people writing wrappers for you.


My goal is to have an html interface

Which would mean implementing significant parts of an HTTP server. That is a big, big, big task -- as in thousands of person hours minimum, and not for beginners. If you want a web interface and you want it done before the end of next year, use an existing http server and write code for that. Here's three suggestions:

  • Apache is the standard workhorse server on linux and the internet generally. You can write code for it in any language you want via CGI, and you can write more integrated modules for it in C++ (but I do not recommend that, it is a little bit overkill). More commonly web interfaces are done with frameworks available for php, python, perl, or ruby.

  • Nginx is written in C++ and I believe you can write web apps for it via modules too, but again that seems like overkill.

  • Node.js is a lightweight server that would probably be perfect for something like this, if you like javascript, since that is its interface. I.e., in this case both the server and client side code would be in js.

If you're truly proficient with js, I recommend the last one. If not, you might as well start learning one of perl/python/ruby and go with apache or nginx (I left php off that list because IMO the other three are more truly general purpose languages, and hence more generally useful to learn).

Because C++ is unnecessary, and probably not advantageous for, writing web applications, it is pretty unusual -- which is why there isn't much in the way of utility libraries for helping with that. It is also much more labour intensive than python/perl/ruby, and so those three (plus php) have come to dominate the niche.

Ideally, the logic and the implementation of something like this, on the server (pi car) side should be independent of communication protocol used. I say this because it means you could develop that on its own first so that it doesn't need a direct relationship to a server at all; you just ssh in and control it that way. You could then develop that into something that will work with a simple custom protocol over sockets that you write a basic TCP/IP server and client for and/or something with a web interface. I.e., the core program/API for controlling the car should be independent such that it could be used with any of those interfaces. So you do that first, probably using ssh for development, and then, since you've left yourself that freedom, decide where to go. The preliminary interface would just be something you can start from the command line and then hit keys for control.

  • OK , so you think using an http server should be the best way? ... HTML si the first goal , next i want make an android app – Forzaferrarileo Sep 22 '13 at 21:52
  • If you want an html interface, then you have to be able to communicate with web browsers, and they use HTTP, so you need an HTTP server. Since the Android API is in java, and java does have a lot of support for web programming, you might consider learning that. It's a lot like C++ but slightly easier to use, although it is still quite labour intensive compared to perl/python/ruby. BUT -- I've made a suggestion you might want to think about in the last paragraph. – goldilocks Sep 23 '13 at 8:01
  • 1
    Definitely suggest node.js as its a lightweight HTTP server- It does not require any other HTTP server. You can write simple JavaScript AJAX to the node.js while the source of node is all C/C++ based wrapped in some JavaScript response and request libraries. Its blazingly fast and definitely a good choice. You can use PhoneGAP build to make a HTML "APP" that can be deployed to all phones. Since the app only needs use semantic HTML5 and JavaScript you don't even need to code natively, saving time. – Piotr Kula Sep 23 '13 at 8:58
  • Thank you , I'LL look for node.js and a c++ rapper for IT – Forzaferrarileo Sep 23 '13 at 16:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.