本帖最后由 qq85168163 于 2013-11-06 18:02:41 编辑

解决方案 »

  1.   

    你把代码
    if(!socket_write($socket, $msg))
    改为 
    if(!socket_write($socket, $msg,strlen($msg)))
    试试。
    如果还是卡。建议你使用strace -p 服务器端进程号。 会有输出内容,贴出来。帮你分析下
      

  2.   


    <?php/*
     * 
     * 客户端
     */class SocketClient {    protected $client;
        protected $message;    public function __construct($domain,$port) {
            $this->init();
            $this->client = stream_socket_client("tcp://$domain:$port", $errno, $errstr, 300000);
            if (!$this->client) {
                $this->log("$errstr ($errno)");
                return FALSE;
            }
            $this->log('client ok');
        }    protected static function init() {
            error_reporting(E_ALL ^ E_NOTICE);
            //error_reporting(0);
            set_time_limit(0);
            ob_implicit_flush();
            date_default_timezone_set('Asia/Shanghai');
            ignore_user_abort(TRUE);
            mb_internal_encoding('gbk');
        }    public function sendMessage($msg) {
            if ($msg === '') {
                return -1;
            }
            try {
                stream_socket_sendto($this->client, $msg);
            } catch (Exception $exc) {
                //$this->log($exc->getTraceAsString());
            }
        }    public function getMessage() {
            $this->message = stream_socket_recvfrom($this->client, 10270000);
            //$this->log("收到消息:");
            //$this->log($this->message);
            fwrite(STDOUT, $this->message . "\r\n");
        }    public function shutdown() {
            stream_socket_shutdown($this->client, STREAM_SHUT_RDWR);
            fclose($this->client);
        }    public static function log($message) {
            echo "\r\n" . $message . "\r\n";
        }}//$client = new SocketClient('127.0.0.1',12345);
        $client->sendMessage($msg);
        echo $client->getMessage();
    }
      

  3.   


    <?php/*
     * 
     * 客户端
     */class SocketClient {    protected $client;
        protected $message;    public function __construct($domain,$port) {
            $this->init();
            $this->client = stream_socket_client("tcp://$domain:$port", $errno, $errstr, 300000);
            if (!$this->client) {
                $this->log("$errstr ($errno)");
                return FALSE;
            }
            $this->log('client ok');
        }    protected static function init() {
            error_reporting(E_ALL ^ E_NOTICE);
            //error_reporting(0);
            set_time_limit(0);
            ob_implicit_flush();
            date_default_timezone_set('Asia/Shanghai');
            ignore_user_abort(TRUE);
            mb_internal_encoding('gbk');
        }    public function sendMessage($msg) {
            if ($msg === '') {
                return -1;
            }
            try {
                stream_socket_sendto($this->client, $msg);
            } catch (Exception $exc) {
                //$this->log($exc->getTraceAsString());
            }
        }    public function getMessage() {
            $this->message = stream_socket_recvfrom($this->client, 10270000);
            //$this->log("收到消息:");
            //$this->log($this->message);
            echo $this->message . "\r\n";
        }    public function shutdown() {
            stream_socket_shutdown($this->client, STREAM_SHUT_RDWR);
            fclose($this->client);
        }    public static function log($message) {
            echo "\r\n" . $message . "\r\n";
        }}//使用方法$client = new SocketClient('127.0.0.1',12345);
    $client->sendMessage($msg);
    echo $client->getMessage();
      

  4.   

    为何在循环内open write close?那不是不断在IO么?send后sleep几秒试试