PHP - 如何确保发送每个套接字请求?

时间:2014-04-23 13:53:45

标签: php sockets zend-framework ubuntu

我的代码从周一到周五上午8点到晚上10点用于打开正门的DOOR和CLOSE DOOR。

我每天都遇到问题PHP无法从上午8点到晚上12点发送命令很好但是之后开始出现奇怪问题,然后修复它我需要总是重启PHP运行机器然后再运行几个小时好了又停止了工作。

在远程位置,当我执行tcpdump跟踪时,我没有得到任何来自PHP的数据包,有时候会有代码,有时我会得到。造成这种情况的原因是什么?

任何人都可以请分享PHP无法发送每个TCP命令请求的原因吗?

联合测试:

$reject = new Application_Model_TCPClient('ip address',58888);
$reject->sendNoWait("connect");

Zend框架:模型:

<?php
class Application_Model_TCPClient {

  private $buffer = null;
  private $fullbuffer = null;
  private $socket = null;
  private $host = null;
  private $post = null;

  public function __construct($host, $port = 58888) {
    $this->host = $host;
    $this->port = $port;
    $this->connect();
  }

  public function __destruct() {
    //$this->disconnect();
  }

  public function connect() {
    $this->socket = @fsockopen($this->host, $this->port, $errno, $errstr, 1);
  }

  public function disconnect() {
    fclose($this->socket);
  }

  public function clearBuffer() {
    $this->buffer = '';
  }

  public function getBuffer() {
    return $this->fullbuffer;
  }

  public function sendNoWait($cmd) {
    if (!$this->socket) {
      $report = "fail";
    } else {
      fputs($this->socket, "$cmd\n");
      $this->fullbuffer = "success";
      $this->disconnect();
      $report = "success";
    }
    switch ($report) {
      case "fail":
        return "fail";
        break;
      case "success":
        return $this->getBuffer();
        break;
    }
  }



}

0 个答案:

没有答案