本文整理汇总了PHP中socket_recv函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_recv函数的具体用法?PHP socket_recv怎么用?PHP socket_recv使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_recv函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
public function run()
{
$null = NULL;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $this->host, $this->port);
socket_listen($socket);
socket_set_nonblock($socket);
$this->clients = array($socket);
//start endless loop
while (true) {
$changed = $this->clients;
socket_select($changed, $null, $null, 0, 10);
//check for new socket
if (in_array($socket, $changed)) {
if (($socket_new = socket_accept($socket)) !== false) {
$this->clients[] = $socket_new;
$header = socket_read($socket_new, 1024);
if ($this->handshake($header, $socket_new) === false) {
continue;
}
socket_getpeername($socket_new, $ip);
if (isset($this->events['open'])) {
$this->events['open']($this, $ip);
}
$found_socket = array_search($socket, $changed);
unset($changed[$found_socket]);
}
}
//loop through all connected sockets
foreach ($changed as $changed_socket) {
//check for any incomming data
while (socket_recv($changed_socket, $buf, 1024, 0) >= 1) {
$received_text = $this->unmask($buf);
//unmask data
$data = json_decode($received_text, true);
//json decode
if (isset($this->events['message'])) {
$this->events['message']($this, $data);
}
break 2;
}
$buf = socket_read($changed_socket, 1024, PHP_NORMAL_READ);
// check disconnected client
if ($buf === false) {
$found_socket = array_search($changed_socket, $this->clients);
socket_getpeername($changed_socket, $ip);
unset($this->clients[$found_socket]);
if (isset($this->events['close'])) {
$this->events['close']($this, $ip);
}
}
}
if ($this->timeout) {
sleep($this->timeout);
}
}
socket_close($socket);
}
示例2: run
public function run()
{
while (true) {
$changed_sockets = $this->allsockets;
@socket_select($changed_sockets, $write = NULL, $exceptions = NULL, NULL);
foreach ($changed_sockets as $socket) {
if ($socket == $this->master) {
if (($ressource = socket_accept($this->master)) < 0) {
$this->log('Socket error: ' . socket_strerror(socket_last_error($ressource)));
continue;
} else {
$client = new Connection($this, $ressource);
$this->clients[$ressource] = $client;
$this->allsockets[] = $ressource;
}
} else {
$client = $this->clients[$socket];
$bytes = @socket_recv($socket, $data, 4096, 0);
if ($bytes === 0) {
$client->onDisconnect();
unset($this->clients[$socket]);
$index = array_search($socket, $this->allsockets);
unset($this->allsockets[$index]);
unset($client);
} else {
$client->onData($data);
}
}
}
}
}
示例3: getinfoToRPi
function getinfoToRPi($command)
{
if (!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [{$errorcode}] {$errormsg} \n");
}
//Connect socket to remote server
try {
$varConnection = socket_connect($sock, '172.17.10.71', 1111);
} catch (Exception $e) {
var_dump($e);
}
if (!$varConnection) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
return False;
}
//Send the message to the server
if (!socket_write($sock, $command, strlen($command))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
return False;
}
$buff = "";
if (False !== ($bytes = socket_recv($sock, $buff, 10, MSG_WAITALL))) {
return $buff;
}
return False;
}
示例4: send
function send()
{
if (!($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [{$errorcode}] {$errormsg} \n");
}
echo "Socket created";
if (!socket_connect($sock, '192.168.0.17', 9000)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [{$errorcode}] {$errormsg} \n");
}
echo "Connection established \n";
$msg = "Hello";
//Send the message to the server
if (!socket_send($sock, $msg, strlen($msg), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [{$errorcode}] {$errormsg} \n");
}
echo "Message send successfully \n";
//Now receive reply from server
if (socket_recv($sock, $buf, 2045, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [{$errorcode}] {$errormsg} \n");
}
//print the received message
$visual = $buf;
socket_close($sock);
}
示例5: index
public function index()
{
error_reporting(~E_WARNING);
$server = '127.0.0.1';
$port = 9999;
if (!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [{$errorcode}] {$errormsg} \n");
}
echo "Socket created \n";
echo 'Enter a message to send : ';
$input = 'abc';
//Send the message to the server
if (!socket_sendto($sock, $input, strlen($input), 0, $server, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [{$errorcode}] {$errormsg} \n");
}
//Now receive reply from server and print it
if (socket_recv($sock, $reply, 5, 256) === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [{$errorcode}] {$errormsg} \n");
}
echo "Reply : {$reply}";
}
示例6: run
/**
*@name run
*@desc wait for the client to connect and process
*/
public function run()
{
while (true) {
$socketArr = $this->sockets;
$write = NULL;
$except = NULL;
socket_select($socketArr, $write, $except, NULL);
//select the socket with message automaticly
//if handshake choose the master
foreach ($socketArr as $socket) {
if ($socket == $this->master) {
$client = socket_accept($this->master);
if ($client < 0) {
$this->log("socket_accept() failed");
continue;
} else {
$this->connect($client);
}
} else {
$this->log("----------New Frame Start-------");
$bytes = @socket_recv($socket, $buffer, 2048, 0);
if ($bytes == 0) {
$this->disconnect($socket);
} else {
$user = $this->getUserBySocket($socket);
if (!$user->handshake) {
$this->doHandShake($user, $buffer);
} else {
$this->process($user, $buffer);
}
}
}
}
}
}
示例7: init
public function init()
{
if (!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [{$errorcode}] {$errormsg} \n");
}
echo "Socket created \n";
//Connect socket to remote server
if (!socket_connect($sock, self::ADDRESS, self::PORT)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect: [{$errorcode}] {$errormsg} \n");
}
echo "Connection established \n";
$message = json_encode(['message' => 'Alright!']);
//Send the message to the server
if (!socket_send($sock, $message, strlen($message), 0)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [{$errorcode}] {$errormsg} \n");
}
echo "Message send successfully \n";
//Now receive reply from server
if (socket_recv($sock, $buf, 2045, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [{$errorcode}] {$errormsg} \n");
}
//print the received message
socket_shutdown($sock);
return $buf;
}
示例8: run
function run()
{
while (true) {
$sockets = $this->sockets;
$write = NULL;
$except = NULL;
socket_select($sockets, $write, $except, NULL);
foreach ($sockets as $sock) {
if ($sock == $this->master) {
$client = socket_accept($this->master);
$this->sockets[] = $client;
$this->users[] = array('socket' => $client, 'handShake' => false);
} else {
$len = socket_recv($sock, $buffer, 2048, 0);
$k = $this->search($sock);
if ($len < 7) {
$this->close($sock);
continue;
}
if (!$this->users[$k]['handShake']) {
$this->handShake($k, $buffer);
} else {
$buffer = $this->uncode($buffer);
$this->send($k, $buffer);
}
}
}
}
}
示例9: recv_all
function recv_all(&$socket, &$recv_buf)
{
$recv_buf = "";
$recv_number = "";
while (true) {
if (!@socket_recv($socket, $recv_buf, 1, 0)) {
return false;
}
if ($recv_buf == ":") {
break;
}
$recv_number = $recv_number . $recv_buf;
}
$recv_buf = "";
$received_bytes = 0;
$temp_recv = "";
// receive all
while (($received_bytes += socket_recv($socket, $temp_recv, (int) $recv_number - $received_bytes, 0)) < $recv_number) {
$recv_buf .= $temp_recv;
if ($received_bytes < 0) {
return false;
}
}
// append the rest
$recv_buf .= $temp_recv;
return true;
}
示例10: __construct
public function __construct($host, $port)
{
$this->host = $host;
$this->port = $port;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($sock, 0, $this->port);
socket_listen($sock);
$clients = array($sock);
while (true) {
$read = $clients;
if (socket_select($read, $write = null, $except = null, 0) < 1) {
continue;
}
if (in_array($sock, $read)) {
$clients[] = $newSocket = socket_accept($sock);
$this->handShake($newSocket);
socket_getpeername($newSocket, $ip);
$this->send(array('type' => 'system', 'message' => $ip . ' Connected'), $clients);
echo "New client connected: {$ip}\n";
$key = array_search($sock, $read);
unset($read[$key]);
}
foreach ($read as $read_sock) {
while (socket_recv($read_sock, $buf, 1024, 0) >= 1) {
$msg = json_decode($this->unmask($buf), true);
$this->send($msg, $clients);
}
}
}
socket_close($sock);
}
示例11: onSocketsAlterados
protected function onSocketsAlterados($socketsRead, $socketsWrite, $socketsExcept)
{
// Caso socket principal foi alterado (Receptor de conexões)
if (in_array($this->socketPrincipal, $socketsRead)) {
$socket = socket_accept($this->socketPrincipal);
if ($socket != null) {
$cabecalho = socket_read($socket, 1024);
$this->tratarSolicitacaoDeConexao($cabecalho, $socket);
}
// Retira socket ouvinte de lista de socketsAlterados
$chave = array_search($this->socketPrincipal, $socketsRead);
unset($socketsRead[$chave]);
}
foreach ($socketsRead as $socketRead) {
// Verifica mensagens recebidas
while (socket_recv($socketRead, $buffer, 1024, 0) >= 1) {
$this->tratarRecebimentoDeMensagem($buffer, $socketRead);
break 2;
}
// Verifica se cliente continua conectado
$buffer = @socket_read($socketRead, 1024, PHP_NORMAL_READ);
if ($buffer === false) {
$this->tratarDesconexao($socketRead);
}
}
}
示例12: revBuff
/**
* 收Buff
* @return bool|mixed|string
* @throws TXException
*/
public function revBuff()
{
$nCnt = @socket_recv($this->handler, $buf, 4, 0);
if ($nCnt === false) {
return -1;
//timeout
}
if ($nCnt != 4) {
return false;
}
$ret = unpack("LLen", $buf);
$len = $ret['Len'];
$data = '';
$recvlen = $len;
$i = 0;
while ($recvlen > 0) {
if (++$i == 100) {
throw new TXException(4003);
}
$nCnt = @socket_recv($this->handler, $buf, $recvlen, 0);
$data .= $buf;
$recvlen -= $nCnt;
}
// $data = chop($data);
if ($result = json_decode($data, true)) {
return $result;
} else {
return $data;
}
}
示例13: negotiate
function negotiate($socket)
{
socket_recv($socket, $buffer, 1024, 0);
for ($chr = 0; $chr < strlen($buffer); $chr++) {
if ($buffer[$chr] == chr(255)) {
$send = isset($send) ? $send . $buffer[$chr] : $buffer[$chr];
$chr++;
if (in_array($buffer[$chr], array(chr(251), chr(252)))) {
$send .= chr(254);
}
if (in_array($buffer[$chr], array(chr(253), chr(254)))) {
$send .= chr(252);
}
$chr++;
$send .= $buffer[$chr];
} else {
break;
}
}
if (isset($send)) {
socket_send($socket, $send, strlen($send), 0);
}
if ($chr - 1 < strlen($buffer)) {
return substr($buffer, $chr);
}
}
示例14: run
function run()
{
while (true) {
$changes = $this->sockets;
socket_select($changes, $write = NULL, $except = NULL, NULL);
foreach ($changes as $sock) {
if ($sock == $this->master) {
$client = socket_accept($this->master);
//$key=uniqid();
$this->sockets[] = $client;
$this->users[] = array('socket' => $client, 'shou' => false);
} else {
$len = socket_recv($sock, $buffer, 2048, 0);
$k = $this->search($sock);
if ($len < 7) {
$name = $this->users[$k]['ming'];
$this->close($sock);
$this->send2($name, $k);
continue;
}
if (!$this->users[$k]['shou']) {
$this->woshou($k, $buffer);
} else {
$buffer = $this->uncode($buffer);
$this->send($k, $buffer);
}
}
}
}
}
示例15: listen
function listen()
{
while (true) {
$changed = $this->sockets;
socket_select($changed, $write = NULL, $except = NULL, NULL);
foreach ($changed as $socket) {
if ($socket == $this->master) {
$client = socket_accept($this->master);
if ($client < 0) {
$this->log("socket_accept() failed");
continue;
} else {
$this->connect($client);
}
} else {
$bytes = @socket_recv($socket, $buffer, 2048, 0);
if ($bytes == 0) {
$this->disconnect($socket);
} else {
$user = $this->getuserbysocket($socket);
if (!$user->handshake) {
$this->dohandshake($user, $buffer);
} else {
$this->process($user, $this->unwrap($buffer));
}
}
}
}
}
}