I have a question regarding the titled question. So, I'm attempting to create a program which passes data/requests for data between a program in C++ and a PHP site running off of an Apache web server.
I've been researching Socket communications, but I'm not understanding the concept. I understand how to create a socket in PHP, and another in c++, and I have them running using a test application. But only individually, not talking to each other OR talking to my web server (the PHP is not on the server at the moment, it is on a different server). So how does this work? From my understanding, you need one to be listening to a port number, and the other one to send something to that command.
Ideally, I would prefer not to use any libraries to help me achieve this. I know this question has been asked many times before, but I'm still getting nowhere.
Could someone provide an explanation of how the above works, or links to a question on here/elsewhere that may be of help? Or if there is a better method of doing this than using sockets? They will be talking to each other a lot, and speed maybe an issue.
Edit, further explanation:
Web server: I'm running an Apache web server. The PHP script is located on this server.
C++ Location: While testing, my c++ application is stored on the same Raspberry Pi that the web server is running on. In the real application, my C++ application will still be stored on the same device (but it won't be a Raspberry Pi - still Linux based though).
Communication: The PHP script will need to be triggered to do things by the C++ script, and vice versa. They will need to both need to pass data (common data structures, but they could be fairly large) each way (so both need to be able to send and receive data).
I think the easiest way is:
1) PHP -> C++ : tcp
2) C++ -> PHP : http
Will try to explain.
1) To take something from C++ app PHP connects to that app using stream_socket_client function. C++ app is listening on host and port using socket, bind and listen functions. As soon as connection arrives C++ app accept it and then in a separate std::thread serves it (recv to get request, send to send response). How to socket.
2) As PHP is working under Apache Web Server, I think the easiest way is use of libcurl library to make HTTP request from C++ app. How to curl.
Another approach could be SOAP or REST Web Services. On both sides, you could provide a SOAP Web Service to exchange data or to call remote functions. On C++-side, you could use Apache Axis to provide a SOAP Server. On PHP side, you can use the build-in SOAPServer class (http://php.net/manual/de/class.soapserver.php).
With SOAP you simply would exchange XML-based messages between both applications over HTTP / HTTPS. With this approch, both applications can trigger each other and exchange data.
Adding to the answer of Ben Schnarr, I think probably the most elegant and easy solution would be to make the C++ program a client for web services implemented in the PHP code. Using sockets directly would force you to have an additional protocol over it to represent the data being transmitted (unless the data is trivially simple, like a stream of ASCII text), which would be a bit like reinventing the wheel.
I'm partial to REST+JSON, because they are simple to implement and use, and very powerful. In contrast, SOAP is quite complex, heavy in resources, and brittle, especially if you use it with C++. One popular implementation I've already used, for instance, would force you to recompile your code every time you change the layout of any of the messages, even if the server is adding a field that the client won't use.
On the PHP side, it's quite easy - it already has all the infrastructure you need. On the C++ program, the minimum you'll need would be an HTTP client library and a JSON codec. I use libcurl for the first (by far the most popular) and jsoncpp for the latter, but there are other options for both. More recently, some libraries appeared with the whole package, but I hadn't had the chance to use them yet. Some examples:
restclient
REST SDK
Restbed
I recommend you to use Unix Sockets
For c++ check this: http://www.linuxhowtos.org/C_C++/socket.htm
For php check this: stream_socket_client
You can pass info internally from both processes.
Hope you can solve it, don't give up.
A SOAP solution could be something that solve the problem, but in my opinion it complicates a lot the application deployment.
You could go down a few levels and work directly with XML-RPC.
Here you have two interesting links, that shows how to implement the client and the server in both languages.
XML-RPC for C++
XML-RPC for PHP
There are other implementatios of XML-RPC for C++ (I have not used this approach with PHP, so I don't know others alternatives), don't stick with only one, try with others and use what you feel more comfortable with.
If you need some guidance on what stas told you then I suggest you look at
http://us2.php.net/manual-lookup.php?pattern=fsock&src={referrer:source?}
http://us2.php.net/manual-lookup.php?pattern=socket&src={referrer:source?}
In C++ you'll want to create a TCP listener that accepts commands (clearly you'll want to put some method of validation in),
then use PHP to open a connection (use fsock or socket your choice - fsock is easier), and write data to your C++ listener. It's ezpz
//updating with some example code
// protocol://ip, port, $errno, $errstr, $timeout
$fp = fsockopen("tcp://ip", 1337, $errno, $errstr, 30);
if(!$fp) die('failed connection or something');
//write something
fwrite($fp, "stuffs to write");
// get a reply?
while (!feof($fp)) {
echo fgets($fp, 128); // where 128 is size
}
I have a c++ application that processes a sentence and responds back to the respective user. I have a website using php that I would like to post data to this application and receive this response. Ideally, the C++ would run on the webserver and not have to be fully loaded each time it is used, but i haven't been able to get a simple 'hello world' (with cgi or c++) to work on the webserver.
What would be the easiest way to integrate a c++ application into a website? Should this work with any webhost?
To Integrate them you can take two approaches.
1) Integrate C++ as a function into PHP with http://www.php-cpp.com
You can create a function, say my_complex_function, that when called in PHP, will execute a C++ code. You can read the site's documentation for information about it.
2) Keep them separate and communicate through HTTP/Pipes/Sockets/Other
You could build a C++ daemon that opens up some kind of communication interface like a Socket, then with PHP you open a socket to it, send the information through the socket, and receive the answer there too.
You can find socket examples for PHP here: http://php.net/manual/en/sockets.examples.php
I have c++ code that can be compiled under Linux, windows or Mac OS. The code compares two images. I would like to have its front end running on a browser and make available to the www.
I am familiar with hosting and dns and that is not the issue. what I can't seem to figure out is:
How do I invoke the script once the image is uploaded by users?
The results from the code needs to be displayed back to the browser. How can a callback be set up for this?
Is there a php solution? Or python (with flask)?
You can either call the C++ application from PHP with exec and then return to browser whatever result is there. This is quick and not good idea. Better approach is to have a service/daemon in C++ running and taking tasks from queue (like RabbitMQ for example). This is scalable solution but requires more effort to implement.
Why don't use CppCMS (http://cppcms.com/) ? CppCMS is a Free High Performance C++ Web Development Framework.
You can use it to handle HTTP request and file upload and easily integrate your code...
You can use sockets, and start listening on some port from C++ program, then from PHP you can connect and send/receive data to/from your program.
I'm currently trying to build a web-base management interface for a cluster of linux servers running a special service. The idea being that the management website can RPC the clusters to gather information, perform configuration tasks, etc.
I've got an XMLRPC Python Server script up and running, and I've written a basic XMLRPC Client in PHP. It all works, but I'm having some serious issues trying to get consistent responses from method calls - Sometimes I get an XML array to n'th depth based on the verbosity of the output of the command I run, sometimes its a XML struct, and sometimes its just a plain old value. Then I've got errors to deal with - It seems the XMLRPC standard has a Fault struct with 'faultCode' and 'faultString' values it responds with, which is great because thats tidy and I can pull that data out via the named key in the XML. But you only get that struct if error is from the shell itself (For example if the called command had a bad argument), whereas an error coming from the command has to be just a normal XML response.
How can I possibly write a robust web-service around what appears to be such varied replies from the XMLRPC server? What I'd really like to do is have the exact same XML struct reply back every time with the same Keys (returnCode (0=success, >1=error), returnValue/s).
Thanks for any advice.
Maybe start with an existing XML/RPC client and let the client do the protocol legwork. There is Zend\XmlRpc in Zend Framework but also standalone clients like fxmlrpc.
I'm trying to determine the best approach to providing an Ajax based terminal using PHP. I haven't made an attempt at writing it yet but having rolled the idea around, the only way I could see it possible, would be 2 scripts:
Script 1; handles Ajax communication
between server and client browser. when a
request is made to use the terminal,
it connects to (or starts as a service then
connects to) Script 2 via a socket.
Script 2; performs the system calls,
passing back output to the Ajax
script for output via the socket.
There are multiple holes I can see in this though, and I'm wondering if anyone has created/seen a set of scripts that can perform these tasks? Any insight would be greatly appreciated!
Thanks :)
Edit: I think I was unclear about a few things. I've found a few scripts that imitate terminals, providing nearly the functionality that I'm looking for, such as AjaxPHPTerm (http://sourceforge.net/projects/ajaxphpterm/)
The problem is that, I'm trying to find a method that permits interaction with shell scripts. If a script prompts Press any key to continue, or Select option [x], using AjaxPHPTerm, it just hangs or drops out of the shell script.
That's why I started thinking sockets, or streams; some way of forming a direct I/O stream to the system calls.
Http is stateless and AJAX, sockets or any other technology based on pages generated by server will not change it magically. Whatever tricks You would use, it will be not efficient and simply not worth the effort (In my opinion at least).
The problem seems to be that AjaxPHPTerm is actually closer to a shell than a terminal (glancing at the code, it seems to do its own CWD handling, and has a simple read-eval-print loop).
Assuming a Posix-compatible OS on the server, the proper way to implement this would probably be to use the pseudo-terminal facility, so that your web terminal appears like a virtual terminal on the system, that running programs can interactively access.