谁有PHP+Socket编程例子,最好是服务器端和客户端应答信息互相通信。各位达人请进

解决方案 »

  1.   


    <?php
    include_once("class.xml2array.php");/*
     * 调用示例
     * $sockObj = new ExecCommand(); //创建对象
     * $sockObj->ExecConnect(); //连接本地SOCKET(port:705)
     * $sockObj->SetCmd("sysadmin listinfo"); //设置要执行的superfw命令
     * $sockObj->SetHeader(); //设置头信息
     * $sockObj->Execute(); //执行命令
     * $rs = $sockObj->GetResult(); //取得结果
     * echo $rs;
     */class ExecCommand
    {
    var $mHost;
    var $mPort;
    var $mTimeout;
    var $fp;
    var $cmd_param;

    function ExecCommand(){
    $this->__Construct();
    } function __Construct ()
    {
    $this->mHost     = "127.0.0.1";
    $this->mPort     = "705";
    $this->mTimeout  = "10";
    } function ExecConnect()
    {
    $this->fp = @fsockopen( $this->mHost, $this->mPort, $errno, $errstr, $this->mTimeout ) or die("请启动/tmp/manage_server服务");
    if ( FALSE == $this->fp )
    {
    echo $errstr;
    exit;
    }
    //$this->Execute();
    } function SetCmd($cmd)
    {
    $rand = $this->MakeRand();

    $cmd_arr = explode(" ", $cmd);

    $this->cmd_param = "cmd命令";
    } function Execute()
    {
    if($this->fp == "")
    {
    return 'create socket first.';
    }
    if ( !fwrite( $this->fp, $this->cmd_param ) )
    {
    return 'FAIL to post cmd data through the socket.';
    }
    return $this->fp;
    } function GetResult()
    {
    while( !feof( $this->fp )){
        $line = fgets($this->fp, 4096);

       if(trim($line)!="")
         $s_response.= $line;
      }
      
    return $s_response;
    } function ExecClose()
    {
    fclose( $this->fp );
    }
    }?>
      

  2.   

    只有客户端的,凑合看看吧$sockObj->SetHeader();                                    //设置头信息
    改成$sockObj->SetCmd(); 上面的我以前写的,没调试,你按需要改改吧