我有一个硬件跟踪设备,我正在使用PHP套接字监听它。我将套接字文件作为守护程序文件运行,这意味着脚本将永远运行。但是在运行了一段时间之后,我得到了这个错误
对等设备重置
连接
我使用的是这个套接字库https://github.com/navarr/Sockets,为了将文件作为守护进程运行,我使用的是forever
运行了一段时间后,我得到了这个错误
<br />
<b>Fatal error</b>: Uncaught exception 'Navarr\Socket\Exception\SocketException' with message 'Connection reset by peer' in /var/www/html/tracker2/src/Socket.php:685
Stack trace:
#0 /var/www/html/tracker2/src/Socket.php(532): Navarr\Socket\Socket::exceptionOnFalse(Resource id #10117, Object(Closure))
#1 /var/www/html/tracker2/src/Server.php(236): Navarr\Socket\Socket->read(1024, 2)
#2 /var/www/html/tracker2/src/Server.php(202): Navarr\Socket\Server->read(Object(Navarr\Socket\Socket))
#3 /var/www/html/tracker2/src/Server.php(158): Navarr\Socket\Server->loopOnce()
#4 /var/www/html/tracker2/socket.php(33): Navarr\Socket\Server->run()
#5 /var/www/html/tracker2/socket.php(96): EchoServer->__construct('172.xx.xx.xxx')
#6 {main}
thrown in <b>/var/www/html/tracker2/src/Socket.php</b> on line <b>685</b><br />
error: Forever detected script exited with code: 255
error: Script restart attempt #1
<br />
<b>Fatal error</b>: Uncaught exception 'Navarr\Socket\Exception\SocketException' with message 'Address already in use' in /var/www/html/tracker2/src/Socket.php:685
Stack trace:
#0 /var/www/html/tracker2/src/Socket.php(138): Navarr\Socket\Socket::exceptionOnFalse(Resource id #28, Object(Closure))
#1 /var/www/html/tracker2/src/Server.php(136): Navarr\Socket\Socket->bind('172.xx.xx.xxx', 8153)
#2 /var/www/html/tracker2/socket.php(20): Navarr\Socket\Server->__construct('172.xx.xx.xxx', 8153)
#3 /var/www/html/tracker2/socket.php(96): EchoServer->__construct('172.xx.xx.xxx')
#4 {main}
thrown in <b>/var/www/html/tracker2/src/Socket.php</b> on line <b>685</b><br />
error: Forever detected script exited with code: 255
我想知道到底是什么导致了这个错误。我不想让我的脚本停止。为什么我得到错误的连接重置由对等。我该如何解决这个问题呢?
如果没有解决方案来修复对等重置的连接,那么有没有可能在收到对等重置的错误连接后5分钟后再次运行脚本。
为什么5分钟是因为套接字进入等待阶段,当永远尝试再次运行脚本时,它会显示地址已在使用中
代码:
<?php
use Navarr\Socket\Socket;
use Navarr\Socket\Server;
class EchoServer extends Server
{
const DEFAULT_PORT = 7;
public function __construct($ip = null, $port = self::DEFAULT_PORT)
{
parent::__construct($ip, $port);
$this->addHook(Server::HOOK_CONNECT, array($this, 'onConnect'));
$this->addHook(Server::HOOK_INPUT, array($this, 'onInput'));
$this->addHook(Server::HOOK_DISCONNECT, array($this, 'onDisconnect'));
$this->run();
}
public function onConnect(Server $server, Socket $client, $message)
{
echo 'Connection Established',"\n";
}
public function onInput(Server $server, Socket $client, $message)
{
echo 'Received "',$message,'"',"\n";
$client->write($message, strlen($message));
}
public function onDisconnect(Server $server, Socket $client, $message)
{
echo 'Disconnection',"\n";
}
}
由于错误出现在行中,下面的685就是在Socket.php文件中编写的函数
protected static function exceptionOnFalse($resource, callable $closure)
{
$result = $closure($resource);
if ($result === false) {
throw new SocketException($resource);
}
return $result;
}
发布于 2018-06-03 07:18:03
出现错误connection reset by peer
的原因是,您的服务器可能正在尝试从已被对等端关闭的套接字中写入或读取消息,实际上,根据异常堆栈跟踪,服务器读取数据时会发生错误。您需要确保您使用的套接字库实现了ping和pong消息。如果客户端发送ping消息,它希望收到pong消息返回或关闭连接。
如果这不能解决您的问题,请尝试捕获异常,退出PHP脚本并重新启动它。可能导致此错误的可能性很多。
https://stackoverflow.com/questions/50659538
复制相似问题