/    Sign up×
Community /Pin to ProfileBookmark

Suggestions for web interface (server) & C++ (client) communication

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);
?>[/code]

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]

[CODE]
/* executeServer.js */

function executeServer()
{
var xhReq = new XMLHttpRequest();
xhReq.open(“POST”, “/php/server.php”, false);
console.log(“out of executeServer”);
}
[/CODE]

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

to post a comment
Full-stack Developer

12 Comments(s)

Copy linkTweet thisAlerts:
@evoluerAug 03.2017 — Hi Simozz,

Is your application time sensitive, meaning should a change in c++ be immediately reflected in the html page?
Copy linkTweet thisAlerts:
@simozzauthorAug 03.2017 — Hello [B]evoluer[/B],

should a change in c++ be immediately reflected in the html page?[/QUOTE]
Yes, this is the idea. My goal is to syncronize the web interface and the C++ application.

Regards,

Simon
Copy linkTweet thisAlerts:
@evoluerAug 03.2017 — One simple solution is to write data to a textfile from c++ and then read data using javascript
Copy linkTweet thisAlerts:
@simozzauthorAug 03.2017 — Hello [B]evoluer[/B],

One simple solution is to write data to a textfile from c++ and then read data using javascript[/QUOTE]
Could you please clarify how do I would accomplish synchronization with a text file ? This is not clear to me.

PS: is it possible to update DOM with fastcgi application ?

Regards,

Simon
Copy linkTweet thisAlerts:
@evoluerAug 03.2017 — 
  • 1. To update DOM, you will need to use javascript, no other option

  • 2. For your project, will the php files be hosted in an online server or a local server like xampp?
  • Copy linkTweet thisAlerts:
    @simozzauthorAug 03.2017 — Hello [B]evoluer[/B],

    I am working on an embedded linux ARM system.

    The webserver I am using is lighttpd.

    There is not any feature that let you open a local server socket (web side) easily and let you interact with DOM content ?

    Regards,

    Simon
    Copy linkTweet thisAlerts:
    @evoluerAug 03.2017 — Oh oh ok, i got it. One last question, are you connecting with the device through serial port or LAN?

    No, there is no direct method/application for this. We need to do some coding on our own.
    Copy linkTweet thisAlerts:
    @simozzauthorAug 03.2017 — Hello,

    Why do we care about the connection type ?

    BTW both C++ application and web interface will run on the same platform and I connect to my board via LAN.

    Regards,

    Simon
    Copy linkTweet thisAlerts:
    @evoluerAug 04.2017 — You are adding the js file as a script source. This will only load the function, but will not execute it.

    1. For testing purpose, have a button and on the button click, execute the function

    2. During production, have the js get executed every 20 or 30 seconds
    Copy linkTweet thisAlerts:
    @simozzauthorAug 04.2017 — Hello [B]evoluer[/B],

    Do you mean execute a server socket every 20 o 30 seconds ?

    If this is the best I can do, I am quiet desolate with web technologies. More than I ever been...

    Regards,

    Simon
    Copy linkTweet thisAlerts:
    @evoluerAug 04.2017 — Hey simozz,

    Web technologies were never meant to access to computer ports / hardwares. Maybe that's the reason you are struck here.

    We did a similar project along these lines.

    1. We created a cronjob with a php file which opens a socket connection and reads data continuously from it

    2. Stored the received data in a database

    3. The webinterface reads data from db every 10 seconds and displays information to the user accordingly
    Copy linkTweet thisAlerts:
    @simozzauthorAug 04.2017 — Hello [B]evoluer[/B],

    You are right. Thank you for your patience.

    I decided, at the moment, that nodejs could fit my needs.

    I opened this thread, hope someone can help with some hint.

    Thank you.

    Simon
    ×

    Success!

    Help @simozz spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...