php socket 的问题

qq85168163 2013-11-06 05:56:37
折腾两天了,望大神们指点:

一下是代码,可以发送socket,服务器端能收到,可是用socket_read接收服务器返回来的消息时就卡在这里了,什么原因呢。。。。

<?php

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket,'192.168.0.34', 6003);
// Write some test data to our socket
$msg = file_get_contents('client4', 'r');//获取一个二进制文件内容
if(!socket_write($socket, $msg))
{
echo("<p>Write failed</p>");
}
// Read any response from the socket
while($buffer = socket_read($socket, 1))
{
//把接收的内容写入名为get4的文件----这个文件没生成
$fp = fopen('get4','w');
fwrite($fp,$buffer);
fclose($fp);
echo("<p>Data sent was: SOME DATA<br> Response was:" . $buffer . "</p>");
break; //执行一次跳出
}
echo("<p>Done Reading from Socket</p>");
?>
...全文
140 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdgdf566 2013-11-07
  • 打赏
  • 举报
回复

<?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();



jdgdf566 2013-11-07
  • 打赏
  • 举报
回复

<?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();
}


ImN1 2013-11-07
  • 打赏
  • 举报
回复
为何在循环内open write close?那不是不断在IO么? send后sleep几秒试试
myzhang222 2013-11-07
  • 打赏
  • 举报
回复
你端口是不是被阻塞了
vcshellcode 2013-11-06
  • 打赏
  • 举报
回复
看下阻塞模式
dragonsea 2013-11-06
  • 打赏
  • 举报
回复
你把代码 if(!socket_write($socket, $msg)) 改为 if(!socket_write($socket, $msg,strlen($msg))) 试试。 如果还是卡。建议你使用strace -p 服务器端进程号。 会有输出内容,贴出来。帮你分析下

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧