Hello,
This is my first post so I hope this is the right section to ask my question.
Please consider I am not an expert on web development, I just have basic knowledge of HTML, JQuery, JS and PHP..
I am working on a project which consists of a C++ application and a small web interface written in HTML and JS, and I need to update a DOM based on a C++ program condition.
The idea is to open a server socket (or a read access pipe) from the web interface and a client socket (could be a write access pipe) from my C++ application, so from C++ I would inform the webserver to update the DOM.
For a simple test I though to implement a server socket from PHP as follows to test the communication and a DOM update (using telnet as a client):
[code=php]<?php
set_time_limit(0);
$address = ‘0.0.0.0’;
$port = 8000;
$buffer = “”;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false)
{
echo “socket_create() failed: reason: ” . socket_strerror(socket_last_error()) . “n”;
}
if (socket_bind($sock, $address, $port) === false)
{
echo “socket_bind() failed: reason: ” . socket_strerror(socket_last_error($sock)) . “n”;
}
if (socket_listen($sock, 1) === false)
{
echo “socket_listen() failed: reason: ” . socket_strerror(socket_last_error($sock)) . “n”;
}
while (true)
{
while ($client = socket_accept($sock))
{
$buffer = socket_read($sock, 20);
if (strlen($buffer) > 0)
{
?>
<script>
document.getElementById(“debugText”).innerHTML = <? echo “$buffer” ?>;
</script>
<?php
}
}
}
socket_close($sock);
?>
The following is my index.html page and JS file to execute the php server
[code=html]
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<meta charset=”utf-8″>
<meta http-equiv=’cache-control’ content=’no-cache’>
<meta http-equiv=’expires’ content=’0′>
<meta http-equiv=’pragma’ content=’no-cache’>
<script type=”text/javascript” src=”executeServer.js”></script>
<title>TEST</title>
</head>
<body>
<div id=”debug”>
<h2 id = “debugText”> </h2>
</div>
</body>
</html>
[CODE]
/* executeServer.js */
function executeServer()
{
var xhReq = new XMLHttpRequest();
xhReq.open(“POST”, “/php/server.php”, false);
console.log(“out of executeServer”);
}
But as expected (due to my basic webdev knowledges), it doesn’t work, neither executing the php file as external file with php command from shell (also due to php file can’t access to js content externally).
The webserver I am using is lighttpd on a linux host.
I understand my solution is wrong or it’s not suitable for this purpose.
Hence, how do I must to modify my code or what is the best solution ?
Thank you in advance for your help.
Regards,
Simon