php socket 用户名密码,socket.php

<?php /*** t_socket** @version 0.1.2* @author Jesus Lara * @copyright 2005-2009 Jesus Lara. All rights reserved.* @filesource* @package tomates* @subpackage socket* @uses operaciones básicas con sockets*/class t_socket {#propertiesprotected $host = 'localhost';protected $port = 80;protected $username = '';protected $passwd = '';private $connected = false;private $errno =0;private $errstr = '';public $timeout = 20;protected $connex_timeout = 20;public $buffer = '';public $lastdata = '';public $status = 0;public $service_status =0;#para socket client php5public $protocol = 'tcp://'; #TCP o UDPpublic $strhost = 'tcp://localhost:80';#contiene el resource handle del socket creadopublic static $connection = null;#datos de la conexion:public $bytes_read = 0;public $bytes_write = 0;#arrays#single methodsfunction status() {return $this->status;}function set_status($status) {$this->status = $status;}function connected() {return $this->connected;}function set_connected($status) {$this->connected = $status;}#constructor#Funcion constructora de la clasefunction __construct($host='', $port='', $timeout='') {$this->host = $host;$this->port = $port;$this->passwd = $password;$this->status = array();$this->connection =null;$this->buffer = null;$this->lastdata = '';if ($timeout!='') {$this->timeout = $timeout;$this->connex_timeout = $timeout;}set_time_limit($this->timeout);//stream_set_timeout//stream_get_meta_data//pfsockopen(string hostname, int port [,int errno] [,string errstr] [,int timeout]); conexion persistente//echo 'constructor ejecutado';}#destructorfunction __destruct() {$this->close();unset($this->connection);}#methodspublic function connect($persistent=false) {if ($persistent) {$this->connection = pfsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);//echo 'usando conexiones persistentes
';} else {$this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);}if (!$this->connection) {echo 'Error: ' . $this->errno . '' . $this->errstr . '';return false;}#defino el tiempo de espera de la conexionstream_set_timeout($this->connection, $this->connex_timeout);if ($this->connection) {#procedo a hacer las evaluaciones necesarias del socket$this->connected = true;$this->service_status = 1;#obtiene un array de estado, timed_out, blocked, eof (end of file), unread_bytes (cantidad de bytes en el buffer interno)#mode (modo de acceso) y uri (direccion actual del recurso)$this->status = stream_get_meta_data($this->connection);return true;} else {return false;}}public function close() {if ($this->connected) {fclose($this->connection);$this->connected = false;}}function read() {if ($this->connected) {socket_set_timeout($this->connection,0,100*1000);$linecount = 0;while ($line = fgets($this->connection)) {$this->buffer[] = trim($line);// DEBUG BLOCK//echo("lastdata = $line;$this->bytes_read = strlen($this->lastdata);$this->status = stream_get_meta_data($this->connection);return $linecount;} else {return false;}}function write($data) {if ($this->connected) {// DEBUG BLOCK//echo("->" . $data . "\n");// END DEBUG BLOCK$nbytes = fwrite($this->connection, $data, strlen($data));if ($nbytes) {$this->bytes_write = $nbytes;return true;} else {return false;}} else {return false;}}function clear_buffer() {$this->buffer = '';unset($this->buffer);return true;}}#end of class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值