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 am trying to write a couple of modules / filter for ejabberd.
After successfully implemented external auth in php I was having high hopes, that I will be able to do everything in PHP.
What I am trying to accomplish is the following:
Ejabberd hooks -> Send data to PHP
PHP accessing Ejabberd via shell_exec() / exec() using ejabberdctl
PHP responding to "Hooks".
I found this SO question (How to filter messages in Ejabberd) which almost answered my question, but it misses the part where the data gets passed to PHP.
So my question are:
How I can send the data to php from erlang (ie. the " %% should return modified Packet or atom drop to drop the packet" part from the linked question)
Where I can find some specification / documentation about ejabberd binary protocol?
Filtering messages in the xmpp internal domain with ejabberd is only possible making an erlang module.
You could try a component approach. The component will be in a subdomain and be able to filter messages passing through it. You should use it like a proxy domain where all parties in the chat should talk to. Remember that your component should be a daemon like process listening on the component interface for stanzas. This is not easy in php. I've used an intermediate python script for this.
Depending on your needs you could also use mod_logxml to dump all stanzas in an xml file and read that file with a daemon.
Actually, I ended up rewriting the whole thing in node.
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 want to write a web application to play chess against the engine Crafty. I'm not new to PHP and javascript, but must learn how to interact with a server process : how can a web application and/or (jQuery) ajax interact bi-directionally with a (linux) program running on the server?
At this moment i am developing on (Apache) local host. Crafty is installed on my Ubuntu PC. This well-known chess engine has no GUI, it runs in terminal by the command
$ /usr/games/crafty
and so you can play chess against it and even see it's calculations :
I can make Crafty run by PHP, using the functions proc_open() or exec(), and most documentation i found states that the output stream should be a file .. But i think i don't want such setup, because then the webpage should be constanty polling that file (eg. by ajax) to see if some new data was appended, right?
How can Crafty talk to the web page directly, saying "i have calculated another variation" or "i have decided a move" etc, then display this info on the web page and let the user give some counter move, just like in terminal. Isn't it possible to use some session / stream / listener?
I have no clue at all, can anybody point me in a right direction?
I recommend you make use of fifos and the & operator - here is why:
You do not want to start crafty on every PHP request, you want to start it only once per game
You don't want to have crafty end at the end of your Request
Your move-requests will want to interact with this allready running instance.
So what I would do is something like:
Prepare a pair of FIFOs using mkfifo - you can do this from PHP or from the shell
On game start, run something like /usr/games/crafty <stdin.fifo >stdout.fifo 2>stderr.fifo &
For your moves, make an AJAX PHP request write to stdin.fifo
For the server moves do long polling with AJAX, on the server side opening stdin.fifo, then stream_select()
Let's say I have a website written in PHP. A user downloads a C++ console program from the website and installs it on their computer. Now, I want to be able to printf() strings from my PHP website (the server) to the C++ console programs (the clients). I have thought of using REST by serving XML file such as:
<prints> <print>Text</print> </prints>
The advantage of REST is it's ease of use. The disadvantage is the C++ program has to constantly download the XML file. Would this be a problem if the user has a limit on their internet usage?
Another solution is sockets which eliminate the issue above, but, they are more complicated and they may be blocked by firewalls/routers.
My question is: Will downloading the XML file from the web server create a problem if the user has a limit on their internet usage? Also, is there a compromise between REST and sockets or is there an alternative to this type of communication?
HTTP is a protocol used to access websites.