【问题标题】:Connection reset by peer php socket对等 php 套接字重置连接
【发布时间】:2018-11-12 13:07:32
【问题描述】:

我有一个硬件跟踪设备,我正在使用 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-&gt;read(1024, 2)
#2 /var/www/html/tracker2/src/Server.php(202): Navarr\Socket\Server-&gt;read(Object(Navarr\Socket\Socket))
#3 /var/www/html/tracker2/src/Server.php(158): Navarr\Socket\Server-&gt;loopOnce()
#4 /var/www/html/tracker2/socket.php(33): Navarr\Socket\Server-&gt;run()
#5 /var/www/html/tracker2/socket.php(96): EchoServer-&gt;__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-&gt;bind('172.xx.xx.xxx', 8153)
#2 /var/www/html/tracker2/socket.php(20): Navarr\Socket\Server-&gt;__construct('172.xx.xx.xxx', 8153)
#3 /var/www/html/tracker2/socket.php(96): EchoServer-&gt;__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;
    }

【问题讨论】:

    标签: php sockets serversocket


    【解决方案1】:

    发生错误connection reset by peer 是因为您的服务器可能正在尝试 从已被对等方关闭的套接字写入或读取消息,实际上,基于异常堆栈跟踪,服务器读取数据时发生错误。如果客户端发送 ping 消息,您需要确保您使用的套接字库实现 ping 和 pong 消息传递,它期望接收 pong 消息或关闭连接。

    如果这不能解决您的问题,请尝试捕获异常,退出 PHP 脚本并重新启动它。导致此错误的可能性有很多。

    【讨论】:

    • 好吧,我正在从硬件设备接收数据,并且在收到消息后没有必要将消息发送回硬件设备。所以我认为 ping 和 pong 消息不适用于我的情况。如何从代码中重新启动 php 脚本。因为如果我退出脚本,我将不得不手动启动它吗?
    • @user1hjgjhgjhggjhg,是硬件连接到您的服务器,还是您是连接到硬件的那个?
    • 我是连接硬件的那个
    • @user1hjgjhgjhggjhg,改用Ratchet WebSockets库,看看问题是否仍然存在。
    • 我必须使用 http 套接字。 Ratchet 用于 websockets
    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 2014-10-31
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2018-08-01
    • 1970-01-01
    相关资源
    最近更新 更多