Connection reset by peer php socket

会有一股神秘感。 提交于 2019-12-25 17:42:58

问题


I have a hardware tracker device and I am listening to it by using PHP sockets. I am running the socket file as daemon file meaning the script is running forever. But after running for a while I am getting this error

Connection reset by peer

I am using this socket Library https://github.com/navarr/Sockets and for running the file as daemon I am using forever

After running for a while I get this error

<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

I want to know exactly what causes this error. I don't want my script to stop. why I am getting error connection reset by peer. How can I fix this

If there is no solution to fix connection reset by peer then is there any possibility that I can run the script again after 5 minutes after getting an error Connection reset by peer.

why 5 minutes is because socket go in wait stage and when forever try to run again the script it says address already in use

Code:

<?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";
    }
}

Since the error is coming in line no 685 below is that function which is written in Socket.php file

protected static function exceptionOnFalse($resource, callable $closure)
    {
        $result = $closure($resource);

        if ($result === false) {
            throw new SocketException($resource);
        }

        return $result;
    }

回答1:


The error connection reset by peer occurred because your server might be trying to write or read a message from a socket that has been closed by the peer, in fact, base on the exception stack trace, the error occurred when the server read data. You need to make sure the socket library your using implement ping and pong messaging if the client sends ping message, it expects to receive pong message back or close the connection.

If that didn't solve your problem, try catching the exception, exit the PHP script and start it again. There are so many possibilities that might cause this error.



来源:https://stackoverflow.com/questions/50659538/connection-reset-by-peer-php-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!