title: AMQPRuntimeException: Error reading data. Received 0 instead of expected 7 bytes
body: <p>It was working, but now it's not working anymore!</p>
  
  <p>I'm using php-amqplib and RabbitMQ.</p>
  
  <p><a href="https://i.stack.imgur.com/g0hC4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g0hC4.jpg" alt=""></a></p>
  
  <p>when I'm  trying to create a new AMQP connection:</p>
  
  <pre><code>$connection = new AMQPConnection('localhost', 5672, 'username', 'password');
  </code></pre>
  
  <p>The code inside the library that is causing this error is:</p>
  
  <pre><code>public function read($n)
  {
      $res = '';
      $read = 0;
  
      while ($read &lt; $n &amp;&amp; !feof($this-&gt;sock) &amp;&amp;
          (false !== ($buf = fread($this-&gt;sock, $n - $read)))) {
  
          if ($buf === '') {
              continue;
          }
  
          $read += strlen($buf);
          $res .= $buf;
      }
  
      if (strlen($res)!=$n) {
          throw new AMQPRuntimeException("Error reading data. Received " .
              strlen($res) . " instead of expected $n bytes");
      }
  
      return $res;
  }
  </code></pre>
  
  <p>When I put this just before the exception:</p>
  
  <pre><code>die($res." :".$n);
  </code></pre>
  
  <p>the result is:</p>
  
  <pre><code>Ï :7 :7
  </code></pre>
  
  <p>it is called twice, in first call $res is two null characters then "Ï"</p>
  
  <p>and in second call it's just null.</p>
  
  <p>oh and I deleted files inside mnesia folder of rabbitmq database manually once, I don't know if that caused the problem, but the RabbitMQ Management which is a web based app running on port 15672 is working fine.</p>