title: Connection reset by peer php socket
body: <p>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 </p>
  
  <blockquote>
    <p>Connection reset by peer</p>
  </blockquote>
  
  <p>I am using this socket Library <a href="https://github.com/navarr/Sockets" rel="nofollow noreferrer">https://github.com/navarr/Sockets</a> and for running the file as daemon I am using <a href="https://www.npmjs.com/package/forever" rel="nofollow noreferrer">forever</a></p>
  
  <p>After running for a while I get this error</p>
  
  <pre><code>&lt;br /&gt;
  &lt;b&gt;Fatal error&lt;/b&gt;:  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-&amp;gt;read(1024, 2)
  #2 /var/www/html/tracker2/src/Server.php(202): Navarr\Socket\Server-&amp;gt;read(Object(Navarr\Socket\Socket))
  #3 /var/www/html/tracker2/src/Server.php(158): Navarr\Socket\Server-&amp;gt;loopOnce()
  #4 /var/www/html/tracker2/socket.php(33): Navarr\Socket\Server-&amp;gt;run()
  #5 /var/www/html/tracker2/socket.php(96): EchoServer-&amp;gt;__construct('172.xx.xx.xxx')
  #6 {main}
    thrown in &lt;b&gt;/var/www/html/tracker2/src/Socket.php&lt;/b&gt; on line &lt;b&gt;685&lt;/b&gt;&lt;br /&gt;
  error: Forever detected script exited with code: 255
  error: Script restart attempt #1
  &lt;br /&gt;
  &lt;b&gt;Fatal error&lt;/b&gt;:  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-&amp;gt;bind('172.xx.xx.xxx', 8153)
  #2 /var/www/html/tracker2/socket.php(20): Navarr\Socket\Server-&amp;gt;__construct('172.xx.xx.xxx', 8153)
  #3 /var/www/html/tracker2/socket.php(96): EchoServer-&amp;gt;__construct('172.xx.xx.xxx')
  #4 {main}
    thrown in &lt;b&gt;/var/www/html/tracker2/src/Socket.php&lt;/b&gt; on line &lt;b&gt;685&lt;/b&gt;&lt;br /&gt;
  error: Forever detected script exited with code: 255
  </code></pre>
  
  <p>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</p>
  
  <p>If there is no solution to fix <strong>connection reset by peer</strong> then is there any possibility that I can run the script again after 5 minutes after getting an error <strong>Connection reset by peer</strong>. </p>
  
  <p>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</p>
  
  <p>Code:</p>
  
  <pre><code>&lt;?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-&gt;addHook(Server::HOOK_CONNECT, array($this, 'onConnect'));
          $this-&gt;addHook(Server::HOOK_INPUT, array($this, 'onInput'));
          $this-&gt;addHook(Server::HOOK_DISCONNECT, array($this, 'onDisconnect'));
          $this-&gt;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-&gt;write($message, strlen($message));
      }
  
      public function onDisconnect(Server $server, Socket $client, $message)
      {
          echo 'Disconnection',"\n";
      }
  }
  </code></pre>
  
  <p>Since the error is coming in line no <strong>685</strong> below is that function which is written in <a href="https://github.com/navarr/Sockets/blob/master/src/Socket.php" rel="nofollow noreferrer">Socket.php</a> file</p>
  
  <pre><code>protected static function exceptionOnFalse($resource, callable $closure)
      {
          $result = $closure($resource);
  
          if ($result === false) {
              throw new SocketException($resource);
          }
  
          return $result;
      }
  </code></pre>