I am looking to setup a C client that can communicate with an index.php file running on a local XAMPP server.
All the tutorials online are based on creating a C client and connecting to a C server. But how would I connect a C client to a PHP file on an xampp server?
Would I need to open sockets on both the C client and also the index.php file using sockets in PHP? Or can I just open a socket in the C program and point it to say localhost/index.php?
The C client will listen for HTTP messages from the server where the index.php will be hosted. The index.php file will contain a html form where I can enter a sentence and the C program will then print that sentence into the computers terminal.
Would it be possible to connect the C program to the server. Then use the exec() command in the index.php form as a POST request and send this to the C program through a socket?
Any help or known articles would be great.
Thank you
It depends what you actually want to achieve, i.e. what you mean by 'connect' a c program to an
index.php
?The first thing you should realize is that what we are talking about here are two processes: (a) the c program and (b) whatever interpreter that executes the PHP script (Probably a web server).
You want to exchange data between two processes, thus we talk about Interprocess Communication.
In the Wikipedia article you see a table listing several means:
Files: Your PHP script could write to a file, the c program read from it (or, under Unix, Pipes would be even better suited)
Sockets: Just as you mentioned already, one of the programs could establish a connection via sockets and exchange their data over this connection.
Since your PHP script is probably run within a web server, the socket solution would be promising: The web server already listens for incoming connections, and provides a decent protocol to exchange data: HTTP .
I will not repeat the answer by Basile , but just give an example for how to exchange some data between the c program and the PHP script with a web server using CGI:
The web server provides a URL, that will invoke the PHP script, like
http://test.me/test.php
.Now, the procedure would be:
The c program sends over a HTTP GET request by transmitting text in the form of
GET /test.php?a=31&b=19 http/1.1
The server will invoke the index.php, providing it with the key value pairs a=31, b=19
The script will do stuff, and print something to stdout like:
abc
(This is not a valid HTTP response, but its sufficient to scetch the procedure in general)
abc
over the connection to the c program.As you see, in this example, you transferred
abc
from the web server/index.php to the c programIn general, the best solution depends on what you want to accomplish. I assume your example is just an example, and you could solve it the way I drafted above. However, if you really just wanted to have something printed to your console, just have your index.php write it to a file/pipe and the c program read the file...
This way or the other, I feel there is much for you to explore ...
Furthermore, I want to clarify one last thing: I used 'c program' for the sake of simplicity, but i need not be a c program, but your problem referrs to an arbitrary process, it could also be a python script run by the python interpreter or ...