【发布时间】:2018-09-13 03:26:57
【问题描述】:
我有一个设备 192.168.1.113 将其数据传输到 192.168.1.100
我希望 PHP 监听这些传入的数据,然后将其提取并输入 MySQL 数据库。
我已经尝试了下面的代码来尝试让数据回显到浏览器中,以查看是否可以看到两者之间正在发送的数据:
<?php
error_reporting(~E_NOTICE);
set_time_limit (0);
$address = "192.168.1.100"; //ip here
$port = 8996; //port number here
$max_clients = 10;
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, $address , 5000) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Waiting for incoming connections... \n";
//array of client sockets
$client_socks = array();
//array of sockets to read
$read = array();
//start loop to listen for incoming connections and process existing
connections
while (true)
{
//prepare array of readable client sockets
$read = array();
//first socket is the master socket
$read[0] = $sock;
//now add the existing client sockets
for ($i = 0; $i < $max_clients; $i++)
{
if($client_socks[$i] != null)
{
$read[$i+1] = $client_socks[$i];
}
}
//now call select - blocking call
if(socket_select($read , $write , $except , null) === false)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
//if ready contains the master socket, then a new connection has come in
if (in_array($sock, $read))
{
for ($i = 0; $i < $max_clients; $i++)
{
if ($client_socks[$i] == null)
{
$client_socks[$i] = socket_accept($sock);
//display information about the client who is connected
if(socket_getpeername($client_socks[$i], $address, $port))
{
echo "Client $address : $port is now connected to us. \n";
}
//Send Welcome message to client
$message = "Welcome to php socket server version 1.0 \n";
$message .= "Enter a message and press enter, and i shall reply
back \n";
socket_write($client_socks[$i] , $message);
break;
}
}
}
//check each client if they send any data
for ($i = 0; $i < $max_clients; $i++)
{
if (in_array($client_socks[$i] , $read))
{
$input = socket_read($client_socks[$i] , 1024);
if ($input == null)
{
//zero length string meaning disconnected, remove and close the
socket
unset($client_socks[$i]);
socket_close($client_socks[$i]);
}
$n = trim($input);
$output = "OK ... $input";
echo "Sending output to client \n";
//send response to client
socket_write($client_socks[$i] , $output);
}
}
}
?>
但它只是挂在浏览器中,并没有做任何事情。我知道有数据会出现,因为我在 Windows 中有一个工具可以监听数据(见图)。
我做错了什么,如何提取这些数据并将其导入我的 MySQL 数据库?
【问题讨论】:
-
我认为您永远不会跳出 @987654324@ 循环,因此您永远不会完成脚本。默认情况下,PHP 仅在脚本完成后发送输出。如果您想要增量输出,请参阅this one 等问题。
-
感谢您抽出宝贵时间提供帮助。您能建议直接更改我的脚本吗?我是新手。
-
不是真的,因为我不知道你到底想做什么。我建议您对输出缓冲和刷新、脚本完成前的输出等进行一些研究。老实说,您上面包含的代码对于说他们是 PHP 新手的人来说是相当先进的。如果这不是你自己写的东西,我建议你停下来,学习基础知识,然后确保你真正理解你在做什么,然后再继续。运行你不理解的代码会让你陷入非常糟糕的境地。
-
我正在尝试获取即将到来的数据(如 Windows GUI 工具中所示),然后将这些数据放入我的网络服务器上的 MySQL 数据库中。不过谢谢你的建议
-
你说的“它只是挂在浏览器中”是什么意思?你如何启动这个脚本?你知道你应该从命令行启动它吗?您能向编写此代码的人寻求帮助吗?