php -------------------- -------------------- -------------------- ---

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
+------------------------------------------------------------------------------
* Socket 基本操作
* <code>
* <?php
* $config = array(
* 'host' => 'localhost',
* 'protocol' => 'tcp',
* 'port' => 80,
* 'timeout' => 30,
* 'persistent' => false,//持久
* );
* $socket = new Socket($config);
* $socket->read(1023);
* </code>
+------------------------------------------------------------------------------
* @version 3.2
* @author WangXian
* @email admin@loopx.cn
* @package libraries
* @creation_date 2010-10-17
* @last_modified 2010-12-25 19:18:47
+------------------------------------------------------------------------------
*/
class Socket
{
protected $_config = array(
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
'persistent' => false,//持久
);
private $config = array();
private $connection = null;
private $connected = false;
private $error = array();
public function __construct($config = array())
{
$this->config = array_merge($this->_config,$config);
if (!is_numeric($this->config['protocol'])) $this->config['protocol'] = getprotobyname($this->config['protocol']);
}
/**
* 连接
* @return object
*/
public function connect()
{
if ($this->connection != null) $this->disconnect();
if ($this->config['persistent'] == true)
{
$tmp = null;
$this->connection = @pfsockopen($this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
}
else
{
$this->connection = fsockopen($this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
}
if (!empty($errNum) || !empty($errStr))
{
$this->error = array('errorStr'=>$errStr,'errorNum'=>$errNum);
}
$this->connected = is_resource($this->connection);
return $this->connected;
}
/**
* 错误信息
* @return string
*/
public function error()
{
return $this->error;
}
/**
* 写数据
* @param string $data
* @return boolean
*/
public function write($data)
{
if (!$this->connected)
{
if (!$this->connect()) return false;
}
return fwrite($this->connection, $data, strlen($data));
}
/**
* 读取数据
* @param integer $length
*/
public function read($length=1024)
{
if (!$this->connected)
{
if (!$this->connect()) return false;
}
if (!feof($this->connection)) return fread($this->connection, $length);
else return false;
}
/**
* 断掉socket连接
* @return boolean
*/
public function disconnect()
{
if (!is_resource($this->connection))
{
$this->connected = false;
return true;
}
$this->connected = !fclose($this->connection);
if (!$this->connected)
{
$this->connection = null;
}
return !$this->connected;
}
public function __destruct()
{
$this->disconnect();
}
}
/* End of file Socket.class.php */
/* Location: ./_framework/libraries/Socket.class.php */