本文整理汇总了PHP中socket_read函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_read函数的具体用法?PHP socket_read怎么用?PHP socket_read使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_read函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: start
public function start()
{
$host = $this->host;
$port = $this->port;
echo "app can be acessed from ws://{$host}:{$port}\n";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
$this->logLastError($socket, "1");
}
if (socket_bind($socket, $host, $port) === false) {
$this->logLastError($socket, "2");
}
if (socket_listen($socket, 5) === false) {
$this->logLastError($socket, "3");
}
$this->setServerSocket($socket);
$serverSocket = $this->getServerSocket();
do {
$sockets = $this->getSockets();
//Set up a blocking call to socket_select
$write = null;
$except = null;
$tv_sec = 5;
if (socket_select($sockets, $write, $except, $tv_sec) < 1) {
continue;
} else {
$this->setSockets($sockets);
}
//Handle new Connections
if ($this->hasSocket($serverSocket)) {
$clientSocket = socket_accept($serverSocket);
if ($clientSocket === false) {
$this->logLastError($serverSocket, "4");
} else {
$this->setClientSocket($clientSocket);
$response = "HTTP/1.1 101 Switching Protocols";
$this->respond($clientSocket, $response);
}
}
//Handle Input
foreach ($this->getClientSockets() as $clientSocket) {
if ($this->hasSocket($clientSocket)) {
$message = socket_read($clientSocket, 2048, PHP_NORMAL_READ);
if ($message === false) {
$this->logLastError($clientSocket, "5");
$this->removeSocket($clientSocket);
socket_close($clientSocket);
break;
} else {
$message = trim($message);
if (!empty($message)) {
$response = $this->trigger("message", ["{$message}"]);
$this->respond($clientSocket, $response);
}
}
}
}
} while (true);
socket_close($serverSocket);
}
示例2: http_request
function http_request($host, $data)
{
if (!($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
echo "socket_create() error!\r\n";
exit;
}
if (!socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1)) {
echo "socket_set_option() error!\r\n";
exit;
}
if (!socket_connect($socket, $host, 80)) {
echo "socket_connect() error!\r\n";
exit;
}
if (!socket_write($socket, $data, strlen($data))) {
echo "socket_write() errror!\r\n";
exit;
}
while ($get = socket_read($socket, 1024, PHP_NORMAL_READ)) {
$content .= $get;
}
socket_close($socket);
$array = array('HTTP/1.1 404 Not Found', 'HTTP/1.1 300 Multiple Choices', 'HTTP/1.1 301 Moved Permanently', 'HTTP/1.1 302 Found', 'HTTP/1.1 304 Not Modified', 'HTTP/1.1 400 Bad Request', 'HTTP/1.1 401 Unauthorized', 'HTTP/1.1 402 Payment Required', 'HTTP/1.1 403 Forbidden', 'HTTP/1.1 405 Method Not Allowed', 'HTTP/1.1 406 Not Acceptable', 'HTTP/1.1 407 Proxy Authentication Required', 'HTTP/1.1 408 Request Timeout', 'HTTP/1.1 409 Conflict', 'HTTP/1.1 410 Gone', 'HTTP/1.1 411 Length Required', 'HTTP/1.1 412 Precondition Failed', 'HTTP/1.1 413 Request Entity Too Large', 'HTTP/1.1 414 Request-URI Too Long', 'HTTP/1.1 415 Unsupported Media Type', 'HTTP/1.1 416 Request Range Not Satisfiable', 'HTTP/1.1 417 Expectation Failed', 'HTTP/1.1 Retry With');
for ($i = 0; $i <= count($array); $i++) {
if (eregi($array[$i], $content)) {
return "{$array[$i]}\r\n";
break;
} else {
return "{$content}\r\n";
break;
}
}
}
示例3: __construct
function __construct($h, $p, $user, $pw)
{
// create server connection
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!socket_connect($this->socket, $h, $p)) {
throw new Exception("Can't communicate with server.");
}
// receive timestamp
$ts = $this->readString();
// Hash container
if (false !== strpos($ts, ':')) {
// digest-auth
$challenge = explode(':', $ts, 2);
$md5 = hash("md5", hash("md5", $user . ':' . $challenge[0] . ':' . $pw) . $challenge[1]);
} else {
// Legacy: cram-md5
$md5 = hash("md5", hash("md5", $pw) . $ts);
}
// send username and hashed password/timestamp
socket_write($this->socket, $user . chr(0) . $md5 . chr(0));
// receives success flag
if (socket_read($this->socket, 1) != chr(0)) {
throw new Exception("Access denied.");
}
}
示例4: send
/**
* Method send
*
* @param string $raw_text
*
* @return string $return_text
*/
public function send($raw_text)
{
if (!empty($raw_text)) {
$this->raw_text = $raw_text;
$xw = new xmlWriter();
$xw->openMemory();
$xw->startDocument('1.0');
$xw->startElement('wordsegmentation');
$xw->writeAttribute('version', '0.1');
$xw->startElement('option');
$xw->writeAttribute('showcategory', '1');
$xw->endElement();
$xw->startElement('authentication');
$xw->writeAttribute('username', $this->username);
$xw->writeAttribute('password', $this->password);
$xw->endElement();
$xw->startElement('text');
$xw->writeRaw($this->raw_text);
$xw->endElement();
$xw->endElement();
$message = iconv("utf-8", "big5", $xw->outputMemory(true));
//send message to CKIP server
set_time_limit(60);
$protocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $protocol);
socket_connect($socket, $this->server_ip, $this->server_port);
socket_write($socket, $message);
$this->return_text = iconv("big5", "utf-8", socket_read($socket, strlen($message) * 3));
socket_shutdown($socket);
socket_close($socket);
}
return $this->return_text;
}
示例5: _tcp_send_request
private static function _tcp_send_request($socket_write_json = null)
{
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (false === $sock) {
echo "sock create error!\n";
}
$address = Config::get("kgi.kgi_mid_server_ip");
$port = Config::get("kgi.kgi_mid_server_port");
try {
$result = socket_connect($sock, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($sock)) . "\n";
exit;
die;
}
socket_write($sock, $socket_write_json, strlen($socket_write_json));
while ($out_str = socket_read($sock, 2048)) {
// echo "revice result\n";
// echo $out_str . "\n";
$json_data = json_decode($out_str);
break;
}
socket_close($sock);
} catch (\ErrorException $e) {
//echo $e->getMessage() . "\n";
}
}
示例6: sendsock
function sendsock($in)
{
global $config;
$service_port = $config['sockport'];
$address = $config['sockhost'];
if (!function_exists("socket_create")) {
error_log(date("y-m-d H:i:s") . " 未启用socket模块\n", 3, "error.log");
return null;
}
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
error_log(date("y-m-d H:i:s") . "socket_create() failed, reason: " . socket_strerror(socket_last_error()) . "\n", 3, "error.log");
return null;
}
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
error_log(date("y-m-d H:i:s") . "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n", 3, "error.log");
return null;
}
socket_write($socket, $in, strlen($in));
$result = socket_read($socket, 8192);
$arr = unpack("C*", $result);
socket_close($socket);
return $arr;
}
示例7: listen
/**
* ソケット待ち受け処理
*/
protected function listen()
{
try {
// 通信が来てないかチェック
$clientsock = @socket_accept($this->sock);
$buf = @socket_read($clientsock, 10240, PHP_NORMAL_READ);
if (false !== $buf and '' != $buf) {
// 通信が来ている
list($topic, $message) = $this->parseBuffer($buf);
if (isset($topic) and isset($message)) {
// チャットへ発言し正常コードを応答(対象チャットが見つからない場合はSkype_Exceptionが発生)
$this->send($topic, $message);
socket_write($clientsock, "200\n", 4);
} else {
// コマンドは受け付けられなかった
socket_write($clientsock, "500\n", 4);
}
}
} catch (Skype_Exception $skype_e) {
// Skype例外(大抵の場合、チャット名が間違っていて取得できなかった)
fputs(STDERR, 'ERROR! Catched Skype_Exception = ' . $skype_e->getMessage() . "\n");
socket_write($clientsock, "501\n", 4);
} catch (Exception $e) {
// それ以外の例外は標準エラー出力を吐いて続行
fputs(STDERR, 'ERROR! Catched Exception = ' . $e->getMessage() . "\n");
}
// 必要があればクライアントのソケットはクローズ
if (is_resource($clientsock)) {
socket_close($clientsock);
}
}
示例8: sendUSD
function sendUSD($text, $pass_server = '000000')
{
$address = gethostbyname('127.0.0.1');
//IP Адрес вашего компьютера
$service_port = 8000;
//Порт
//$pass_server='000000'; //Пароль
$phone = preg_replace('/^\\+/', '', $phone);
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
echo "socket create failed reason: " . socket_strerror($socket) . "\n";
}
$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
echo "socket connect failed.\nReason: ({$result}) " . socket_strerror($result) . "\n";
}
$text = iconv("UTF-8", "Windows-1251", $text);
$in = base64_encode($pass_server . "#CMD#[USSD]" . $text);
//Пример отправки смс
//$in = base64_encode($pass_server."#CMD#[USSD]*102#"); //Пример запроса USSD команды
$out = '';
socket_write($socket, $in, strlen($in));
//echo "Response:\n\n";
$res = '';
while ($out = socket_read($socket, 2048)) {
$res .= $out;
}
socket_close($socket);
$res = iconv("Windows-1251", "UTF-8", $res);
if (preg_match('/USSD-RESPONSE\\[.+?\\]:(.+)/is', $res, $m)) {
$res = $m[1];
}
return $res;
}
示例9: read
public function read($length = 1024)
{
$data = '';
while (true) {
$read = array($this->socket);
$write = array();
$except = array();
$r = socket_select($read, $write, $except, 0, self::TIMEOUT_USEC);
if (false === $r) {
throw new PhpBuf_RPC_Socket_Exception('socket select fail: ' . socket_strerror(socket_last_error()));
}
if (0 === $r) {
continue;
}
$result = @socket_read($this->socket, $length, PHP_BINARY_READ);
if (false === $result) {
throw new PhpBuf_RPC_Socket_Exception('read error:' . socket_strerror(socket_last_error()));
}
if (empty($result)) {
break;
}
$data .= $result;
}
return $data;
}
示例10: read
/**
* Read from the socket
*
* @return string the information read from the socket
*
* @throws SocketException when the socket disconnects or the connection is lost
*/
public function read()
{
if (false === ($buf = @socket_read($this->socket, 2048, PHP_NORMAL_READ))) {
throw new SocketException();
}
return $buf;
}
示例11: Read
function Read()
{
if ($this->Connected) {
return socket_read($this->Socket, 1024);
}
return null;
}
示例12: _checkClosedRequests
protected function _checkClosedRequests()
{
foreach ($this->_clients as $key => $client) {
if (in_array($client, $this->_read)) {
$buf = @socket_read($client, 1024, PHP_NORMAL_READ);
if ($buf === false) {
throw new Exception\Listener(socket_strerror(socket_last_error($client)));
}
$buf = trim($buf);
if ($buf) {
switch ($buf) {
case 'quit':
print "Close connection\n";
unset($this->_clients[$key]);
$this->_requests[$key]->finished = true;
unset($this->_requests[$key]);
socket_close($client);
break;
case 'shutdown':
throw new Exception\Listener('Shutdown detected');
}
}
}
}
return true;
}
示例13: await
private function await()
{
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($sock < 0) {
echo "Error:" . socket_strerror(socket_last_error()) . "\n";
}
$ret = socket_bind($sock, $this->ip, $this->port);
if (!$ret) {
echo "BIND FAILED:" . socket_strerror(socket_last_error()) . "\n";
exit;
}
echo "OK\n";
$ret = socket_listen($sock);
if ($ret < 0) {
echo "LISTEN FAILED:" . socket_strerror(socket_last_error()) . "\n";
}
do {
$new_sock = null;
try {
$new_sock = socket_accept($sock);
} catch (Exception $e) {
echo $e->getMessage();
echo "ACCEPT FAILED:" . socket_strerror(socket_last_error()) . "\n";
}
try {
$request_string = socket_read($new_sock, 1024);
$response = $this->output($request_string);
socket_write($new_sock, $response);
socket_close($new_sock);
} catch (Exception $e) {
echo $e->getMessage();
echo "READ FAILED:" . socket_strerror(socket_last_error()) . "\n";
}
} while (TRUE);
}
示例14: send
public function send($data)
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket == null) {
return false;
}
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $this->timeout, 'usec' => 0));
if (!socket_connect($socket, $this->host, $this->port)) {
return false;
}
$packed = json_encode($data);
echo $packed;
//Write Header
socket_write($socket, "ZBXD");
//Write Data length
$length = strlen($packed);
socket_write($socket, pack("V*", $length, $length >> 32));
socket_write($socket, $packed);
$return_header = socket_read($socket, 5);
$return_length = unpack("V*", socket_read($socket, 8));
$return_length = $return_length[1] + ($return_length[2] << 32);
$return_data = socket_read($socket, $return_length);
$return_data = json_decode($return_data);
socket_close($socket);
return $return_data;
}
示例15: handleRead
protected function handleRead($read_sock)
{
// read until newline or 1024 bytes
// socket_read while show errors when the client is disconnected, so silence the error messages
$data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
// check if the client is disconnected
if ($data === false) {
// remove client for $this->clients array
$key = array_search($read_sock, $this->clients);
unset($this->clients[$key]);
echo "client disconnected.\n";
// continue to the next client to read from, if any
continue;
}
// trim off the trailing/beginning white spaces
$data = trim($data);
// check if there is any data after trimming off the spaces
if (!empty($data)) {
// send this to all the clients in the $this->clients array (except the first one, which is a listening socket)
foreach ($this->clients as $send_sock) {
// if its the listening sock or the client that we got the message from, go to the next one in the list
if ($send_sock == $this->sock || $send_sock == $read_sock) {
continue;
}
// write the message to the client -- add a newline character to the end of the message
socket_write($send_sock, $data . "\n");
}
}
}