简体   繁体   English

同一台机器上的套接字连接,PHP到C ++并返回

[英]Socket connection on same machine, PHP to C++ and back

I had to implement a communication between an application written in c++ and a web server scripted with PHP. 我必须在用c ++编写的应用程序和用PHP编写脚本的Web服务器之间实现通信。 The basic idea was to create a socket with the c++ application, binding it and listen for the PHP connection to it. 基本思想是使用c ++应用程序创建一个套接字,绑定它并监听与它的PHP连接。 The PHP would then send a message over TCP asking for data and the c++ would send back the answer. 然后,PHP将通过TCP发送消息,要求提供数据,然后c ++将发回答案。 Single header request, single string(JSON) answer. 单头请求,单字符串(JSON)答案。 So far, so good. 到现在为止还挺好。 This is the code I used for the PHP side: 这是我用于PHP方面的代码:

<?php
error_reporting(E_ALL);

$service_port = 8080;
$address = 'localhost';


$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} 

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} 


$in = "request1";

$out = '';

socket_write($socket, $in, strlen($in));

$buf = 'This is my buffer.';
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
    echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
    echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);

echo $buf . "\n";
//elaborate the $buf
?>

Now I would like to implement also an auto-update of the data, with the c++ (server side) sending data and the PHP side(client side) to collect them. 现在我想实现数据的自动更新,c ++(服务器端)发送数据,PHP端(客户端)收集它们。 The data update should be done every minute. 数据更新应该每分钟完成一次。 Sadly I don't have much experience with web developing, so I'm kindly asking some advices on that. 可悲的是,我没有太多的网络开发经验,所以我很乐意就此提出一些建议。

The easiest thing I could do was looping the PHP code untill a known message got received. 我能做的最简单的事情是循环PHP代码,直到收到已知的消息。 The problem I faced is that I don't get data untill the loop is over and the PHP script ends. 我遇到的问题是,我没有得到数据,直到循环结束,PHP脚本结束。 To overcome the problem I tried to set PHP side the socket to be non-blocking replacing in the socket_recv the option MSG_WAITALL with MSG_DONTWAIT but nothing changed. 为了解决这个问题,我尝试将套接字设置为非阻塞替换套接字,用MSG_DONTWAIT选项MSG_WAITALL替换,但没有改变。 Then I tried to break out of the PHP script, but it can't be done since I need to cycle it to get the data every second. 然后我试图打破PHP脚本,但由于我需要循环它以每秒获取数据,所以无法完成。 Plus I get another problem, during the loop cycles, the apache server the PHP is running on is getting a 503 error, Server Unavailable. 另外我得到另一个问题,在循环周期中,运行PHP的apache服务器出现503错误,服务器不可用。 I don't know why it happens, maybe the received message buffer is full, or the script takes too many resources. 我不知道它为什么会发生,也许收到的消息缓冲区已满,或者脚本占用了太多资源。 Due to my lack of experience I can't understand why. 由于我缺乏经验,我无法理解为什么。 I know there are good libraries to perform what I need but I'm working on an embedded machine so I'm limited to work with basic libraries. 我知道有很好的库可以执行我需要的东西,但我正在使用嵌入式机器,因此我只能使用基本库。

How can I achieve the PHP on the Apache server to get timed data from the c++ application ? 如何在Apache服务器上实现PHP以从c ++应用程序获取定时数据? What am I overlooking ? 我在俯瞰什么? Thanks a lot in advance for your help. 非常感谢您的帮助。

Edit: Deleted rhetorical question. 编辑:删除修辞问题。

Using php to obtain data from another process via tcp/ip will require a bit more effort compared to sending data from a php script executed via apache to another process using tcp/ip sockets. 与使用tcp / ip套接字将数据从通过apache执行的php脚本发送到另一个进程相比,使用php通过tcp / ip从另一个进程获取数据需要更多的努力。

Your php script will need to be started outside of apache and will require an infinite loop that continuously listens to the given port to read input data. 您的PHP脚本需要在apache之外启动,并且需要一个无限循环,持续监听给定端口以读取输入数据。 The following post outlines what I believe you are after. 以下帖子概述了我相信你所追求的。

EDIT 编辑

In order to display the results on the web page you could use an in memory data store such as Redis. 为了在网页上显示结果,您可以使用内存数据存储,例如Redis。 The php server application would update a data structure in redis, then the webpage onload could pull the latest value from redis and display it on screen, however this would require a page refresh. php服务器应用程序将以redis更新数据结构,然后网页onload可以从redis中提取最新值并将其显示在屏幕上,但这需要页面刷新。

If you require that the data be refreshed immediately once received without server postback, this will require some javascript. 如果您要求在没有服务器回发的情况下立即刷新数据,则需要一些javascript。 When a user loads the page you would initialize a websocket connection with a websocket server (people generally use node.js and socket.io for implementing this but you can code a websocket server in anything that supports the technology). 当用户加载页面时,您将使用websocket服务器初始化websocket连接(人们通常使用node.js和socket.io来实现此功能,但您可以在支持该技术的任何内容中编写websocket服务器代码)。 The websocket server would be subscribed to a redis channel that when updated would send the new values to all connected clients. websocket服务器将订阅redis频道,该频道在更新时会将新值发送给所有连接的客户端。 I suppose this could be done without redis as well depending on what your requirements are. 我想这可以在没有redis的情况下完成,具体取决于您的要求。 The program for the websocket server could also be the server that accepts the information from your c++ program using a separate thread. websocket服务器的程序也可以是使用单独的线程接受来自c ++程序的信息的服务器。 When data comes in from the c++ program you could then send it directly to the clients connected via websocket. 当数据从c ++程序进入时,您可以将其直接发送到通过websocket连接的客户端。

Don't reinvent the wheel. 不要重新发明轮子。 Use a websockets. 使用websockets。 It's designed to do this async communication and you can send JSON over it just fine. 它旨在执行此异步通信,您可以通过它发送JSON就好了。

There are libraries for C++ and PHP, meaning this should be pretty easy. 有C ++和PHP库,这意味着这应该很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM