php用socket获取数据,php socket获取数据类

* Socket class

*

*

* @author ZT*/

ClassSocket

{private static $instance;private $connection = null;private $connectionState =DISCONNECTED;private $defaultHost = "127.0.0.1";private $defaultPort = 80;private $defaultTimeout = 20;public $debug = false;function__construct()

{

}/**

* Singleton pattern. Returns the same instance to all callers

*

* @return Socket*/

public static functionsingleton()

{if (self::$instance == null || ! self::$instanceinstanceof Socket)

{

self::$instance = newSocket();

}return self::$instance;

}/**

* Connects to the socket with the given address and port

*

* @return void*/

public function connect($serverHost=false, $serverPort=false, $timeOut=false)

{if($serverHost == false)

{$serverHost = $this->defaultHost;

}if($serverPort == false)

{$serverPort = $this->defaultPort;

}$this->defaultHost = $serverHost;$this->defaultPort = $serverPort;if($timeOut == false)

{$timeOut = $this->defaultTimeout;

}$this->connection = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);if(socket_connect($this->connection,$serverHost,$serverPort) == false)

{$errorString = socket_strerror(socket_last_error($this->connection));$this->_throwError("Connecting to {$serverHost}:{$serverPort} failed.
Reason: {$errorString}");

}else{$this->_throwMsg("Socket connected!");

}$this->connectionState =CONNECTED;

}/**

* Disconnects from the server

*

* @return True on succes, false if the connection was already closed*/

public functiondisconnect()

{if($this->validateConnection())

{

socket_close($this->connection);$this->connectionState =DISCONNECTED;$this->_throwMsg("Socket disconnected!");return true;

}return false;

}/**

* Sends a command to the server

*

* @return string Server response*/

public function sendRequest($command)

{if($this->validateConnection())

{$result = socket_write($this->connection,$command,strlen($command));return $result;

}$this->_throwError("Sending command \"{$command}\" failed.
Reason: Not connected");

}public functionisConn()

{return $this->connectionState;

}public functiongetUnreadBytes()

{$info = socket_get_status($this->connection);return $info[‘unread_bytes‘];

}public function getConnName(&$addr, &$port)

{if ($this->validateConnection())

{

socket_getsockname($this->connection,$addr,$port);

}

}/**

* Gets the server response (not multilined)

*

* @return string Server response*/

public functiongetResponse()

{$read_set = array($this->connection);while (($events = socket_select($read_set, $write_set = NULL, $exception_set = NULL, 0)) !== false)

{if ($events > 0)

{foreach ($read_set as $so)

{if (!is_resource($so))

{$this->_throwError("Receiving response from server failed.
Reason: Not connected");return false;

}elseif ( ( $ret = @socket_read($so,4096,PHP_BINARY_READ) ) == false){$this->_throwError("Receiving response from server failed.
Reason: Not bytes to read");return false;

}return $ret;

}

}

}return false;

}public functionwaitForResponse()

{if($this->validateConnection())

{$socketResponse = "";while($val = socket_read($this->connection, 1024)){$socketResponse .= $val;

}return $socketResponse;

}$this->_throwError("Receiving response from server failed.
Reason: Not connected");return false;

}/**

* Validates the connection state

*

* @return bool*/

private functionvalidateConnection()

{return (is_resource($this->connection) && ($this->connectionState !=DISCONNECTED));

}/**

* Throws an error

*

* @return void*/

private function _throwError($errorMessage)

{echo "Socket error: " . $errorMessage;

}/**

* Throws an message

*

* @return void*/

private function _throwMsg($msg)

{if ($this->debug)

{echo "Socket message: " . $msg . "\n\n";

}

}/**

* If there still was a connection alive, disconnect it*/

public function__destruct()

{$this->disconnect();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值