0

The question:

Is it possible to run a C++ library on a HTTP server and access it via a web service in another language?

In more detail:

A few years back I did some development work using C# where I connected and used a Web service, I made a connected and accessed the web service methods such as:

var myservice = service.getResults(); 

Which would return all of the results from the database.. I want to do something similar in C++ in that, I have a C++ library that handles all of the database connections and provides a series of methods in order to access. I now want to open this up to a web method.

The issue:

I don't know how I would go about running this on a HTTP server and accessing using methods that are in different languages? For example, a few of our clients use PHP whereas some use Python. I want to be able to access the methods from each of these languages, so, for example (pseudo code(

PHP:

// Connect to API (Web Service)  e.g. http://localhost:8000
$apiClient = new WebService::API();
$results = $apiClient->getResults();

This would then call (in C++):

function getResults() 
{
    // Run DB query 
}

The implementation of the methods I am not struggling with. It's more to do with how do I make this appear on a HTTP server and can I use something like SOAP in order to connect and provide the functionality? I just want hints or resources in order to do this.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Phorce
  • 4,424
  • 13
  • 57
  • 107
  • yes. http is your protocol and independent of language used. All you need is a format for information interchange. JSON or XML for example. Have a look at this: http://stackoverflow.com/questions/6542676/web-services-in-c-and-c – Marged Oct 16 '15 at 21:16
  • @marged - thank you for the reply. Would it be a massive project to implement on your own without third party tools? I already have the methods.. I just need to expose them as a web service, if this makes sense? I know i would need to covert the result into json before serving it back – Phorce Oct 16 '15 at 21:19
  • Depends on your definition of "massive". I personally would not do the basic tcp / http stuff on my own. – Marged Oct 16 '15 at 21:21
  • @marged - wait so.. I wouldn't just run ./main (for example) on my web server and then provide a socket and listen in for incoming connections? – Phorce Oct 16 '15 at 21:33
  • you have to implement the http protocol over this socket. This can be done but depending on which other prerequisites you have you will end up implementing multithreading and tls. This would keep you busy for a while ;-) Because of this I would use a framework / library from the beginning. Or switch to a different language that is more "web-like" – Marged Oct 16 '15 at 21:37
  • @marged - yes, makes sense now. That would be a lot of work. So therefore, would something like boost:asio be a good starting point to implementing what I want to do ? Thanks for your help :) – Phorce Oct 16 '15 at 21:43
  • 1
    I don't know much about boost, but I would say asio alone is too low-level. If you want to stick to boost you could look at something like this:http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/examples/cpp11_examples.html#boost_asio.examples.cpp11_examples.http_server. If I were you I would do more searching on stack overflow for examples how to create webservices with C++ – Marged Oct 16 '15 at 21:46

0 Answers0