无聊写的一个PHP Socket类

无聊写的一个PHP Socket类,功能还比较简单,不完善.
None.gif <? php
ExpandedBlockStart.gifContractedBlock.gif
/** */ /***
InBlock.gif     * @project:socket类
InBlock.gif     * @license:GPL
InBlock.gif     * 
@author: 8th@live.cn-星期八
InBlock.gif     * @description:PHP Socket协议异步通信
InBlock.gif     * @file:socket.class.php
InBlock.gif     * @created date:Fri Apr 25 12:22:33 CST 2008
InBlock.gif     * @last modified :
ExpandedBlockEnd.gif     **
*/

None.gif
class  sockets
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public $host;//通信地址
InBlock.gif
    public $port;//通信端口
InBlock.gif
    public $limitTime=0;//连接超时时间
InBlock.gif
    public $backlog=3;//请求队列中允许的最大请求数
InBlock.gif
    private $socket=null;
InBlock.gif    
private $result=null;
InBlock.gif    
private $spawn=null;
InBlock.gif    
private $input=null;
InBlock.gif    
//构造函数
InBlock.gif
    public function __construct()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        set_time_limit($
this->limitTime);
InBlock.gif        $
this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//创建Socket连接,监听外部连接
InBlock.gif
    public function socket_bind_listen()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        $
this->result = socket_bind($this->socket, $this->host, $this->port) or die("Could not bind to socket\n");
InBlock.gif        $
this->result = socket_listen($this->socket, $this->backlog) or die("Could not set up socket listener\n");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//接受请求连接,调用socket处理信息
InBlock.gif
    public function accept_client()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        $
this->spawn = socket_accept($this->socket) or die("Could not accept incoming connection\n");
InBlock.gif        $msg 
= "Welcome to the Test Server";
InBlock.gif        socket_write($
this->spawn, $msg, strlen($msg));
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 读取客户端输入 
InBlock.gif
    public function read_client()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        $
this->input = socket_read($this->spawn, 2048, PHP_NORMAL_READ) or die("Could not read input\n"); 
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 处理客户端输入并返回数据 
InBlock.gif
    public function write_client()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        socket_write($
this->spawn, $this->input, strlen ($this->input)) or die("Could not write output\n");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//析构函数
InBlock.gif
    public function __destruct()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        socket_close($
this->spawn); 
InBlock.gif        socket_close($
this->socket); 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/** */ /***
InBlock.gif$socket_test=new sockets();
InBlock.gif$socket_test->host="192.168.0.2";
InBlock.gif$socket_test->port="13654";
InBlock.gif$socket_test->socket_bind_listen();
InBlock.gif$socket_test->accept_client();
InBlock.gif$socket_test->read_client();
InBlock.gif$socket_test->write_client();
ExpandedBlockEnd.gif     **
*/

None.gif
?>

转载于:https://www.cnblogs.com/8th/archive/2008/04/23/socket.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值