PHP反复从TCP套接字接收数据 [英] PHP receiving data from TCP socket repeatedly
问题描述
这个想法是通过TCP套接字连接PHP网页和C程序,以便该网页等待来自C程序的连接,并在建立连接后立即接收数据. PHP套接字连接和数据接收的代码如下:
The idea is to connect PHP webpage and C program via TCP socket so, that webpage waits for connection from C program and receives data as soon as connection established. The code of PHP socket connection and data receiving is below:
PHP(socketRead.php):
PHP(socketRead.php):
$address = 'localhost';
$port = 5001;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo "Socket creation error";
}
else {
echo "Socket created <br/>";
}
if (($ret = socket_bind($sock, $address, $port)) < 0) {
echo "Host/port connection failed";
}
else {
echo "Host/port connection successful <br/>";
}
if (($ret = socket_listen($sock, 5)) < 0) {
echo "Socket error";
}
else {
echo "Waiting connection <br/>";
}
if (($msgsock = socket_accept($sock)) < 0) {
echo "Socket connection start error";
} else {
echo "Awaiting data <br/>";
}
//Connection established, reading data
if (false === ($buf = socket_read($msgsock, 1024))) {
echo "Read error";
} else {
echo "Data: ".$buf;
}
if (isset($sock)) {
socket_close($ret);
socket_close($sock);
}
echo "<br /> Socket closed";
要从套接字动态读取数据,我使用了jquery请求.
To read data from socket dynamically I use jquery request.
Javascript(index.html):
Javascript(index.html):
function update_content() {
var request = $.get("socket/socketRead.php");
request.success(function(result) {
document.write(result);
});
}
update_content();
效果很好,直到我尝试增加间隔以重复读取数据.
Worked great until I tried to add interval to read data repeatedly.
Javascript(index.html):
Javascript(index.html):
var timer = setInterval(function(){update_content();}, 1000);
第一次连接/接收仍然有效,但是当我尝试再次连接时,我在C程序中收到"Connection rejected".您能帮我解决这个问题吗?
The first connection/receiving is still works,but then I get "Connection refused" in C program when trying to connect again. Can you help me with this problem?
推荐答案
您可以为此使用SSE.
You could use SSE for that.
服务器发送事件
https://developer.mozilla.org/zh-CN/docs/Web/API/EventSource
这不能回答您的php问题.. 但SSE是针对您要尝试做的事情.
this does not answer your php question .. but SSE it's made for what you are trying to do.
js
var sse=new EventSource("sse.php");
sse.onmessage=function(e){
console.log(e.data)
};
sse.php
function send($data){
echo "id: ".time().PHP_EOL;
echo "data: ".$data.PHP_EOL;
echo PHP_EOL;
ob_flush(); // clear memory
flush();
}
header('Content-Type: text/event-stream'); // specific sse mimetype
header('Cache-Control: no-cache'); // no cache
$address='localhost';$port=5001;
while(true){
$msg=($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))?'created':'error';
send("Socket creation ".$msg);
$msg=($ret = socket_bind($sock, $address, $port))?'connected':'refused';
send("connection ".$msg);
//....
// do the rest
//.....
sleep(10);
}
note1:不确定php语法是否正确,但这只是在这里给您一个想法.
note1:Not sure if the php syntax is correct but it's just here to give you an idea.
sse ..第二部分的另一个示例也使用json.
Another example of sse .. 2nd part is also using json.
https://stackoverflow.com/a/20689738/2450730
这篇关于PHP反复从TCP套接字接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!