我有一个设备192.168.1.113,它将数据传输到192.168.1.100
我希望PHP能够收听这些传入的数据,然后将其解压缩并输入MySQL数据库。
我已经尝试过以下代码尝试让数据回显到浏览器中,以便查看是否可以看到两者之间发送的数据:
mListView.setAdapter(mAdapter);
但它只是挂在浏览器中并且什么都不做。我知道有数据出现,因为我在Windows中有一个监听数据的工具(见图)。
我做错了什么,如何提取这些数据并将其放入我的MySQL数据库?
答案 0 :(得分:1)
二进制socket_read正在阻止,请参阅http://php.net/manual/en/function.socket-read.php的前2条评论。他们都确认PHP_BINARY_READ socket_read正在阻塞。
我建议使用带有读取超时的socket_select。 一些片段:
$defaultRecvTimeout=750;
socket_set_option($socket6,SOL_SOCKET,SO_RCVTIMEO,$this->millisecToSolArray(self::$defaultRecvTimeout));
// wait for data to be available, up to timeout
$r1 = array($this->socket);
$w = null;
$e = array($this->socket);
$readTimeout = socket_get_option($this->socket,SOL_SOCKET,SO_RCVTIMEO);
$res = socket_select($r1,$w,$e,$readTimeout['sec'],$readTimeout['usec']);
// check
if ($res === false) throw new SocketTransportException('Could not examine socket; '.socket_strerror(socket_last_error()), socket_last_error());
if (!empty($e)) throw new SocketTransportException('Socket exception while waiting for data; '.socket_strerror(socket_last_error()), socket_last_error());
if (empty($r1)) return false; // Nothing to read, return;
$d = "";
$r = 0;
$buf = '';
$r += socket_recv($this->socket,$buf,1000,MSG_DONTWAIT);
if ($r === false) throw new SocketTransportException('Could not read '.$length.' bytes from socket; '.socket_strerror(socket_last_error()), socket_last_error());
$d .= $buf;
return $d;
这不是我的代码,我发现它在某个地方,不幸的是忘记了谁归功于它。但是sockettransport.class.php是一个很好的套接字处理类:https://github.com/paulv888/cronjobs/blob/3f8683ed1dff0a343dff7114fa2edc50a525c190/myclasses/sockettransport.class.php
实际上原文来自这里:https://github.com/onlinecity/php-smpp