note 99259 added to function.socket-create-listen
| From: | danfatspeetdotcom at osu1 dot php dot net | Date: | Fri, 06 Aug 2010 12:50:18 +0000 |
| Subject: | note 99259 added to function.socket-create-listen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171427@lists.php.net to get a copy of this message | ||
A simple Class for Socket Server.
<?php
/**
* Description of socketclass
*
* @author daniell
*/
class Socket {
// Sockets
private $sock;
private $lister;
// Wait, Porta, Ack
private $wait;
private $porta;
private $ack;
// Usuario
private $user;
function __construct($wait, $porta, $ack) {
$this->wait = $wait;
$this->porta = $porta;
$this->ack = $ack;
$this->user['ip'] = $this->user['port'] = NULL;
// Cria o servidor
$this->socketListen();
}
function __destruct() {
$this->close();
}
// Cria o Servidor
public function socketListen(){
while( !( $this->lister = @socket_create_listen($this->porta) ) ){
echo 'Aguarde..';
sleep($this->wait);
}
}
// Aguarda um Cliente conectar.
public function waitClient(){
$r = array($this->lister);
if( socket_select($r,$w = NULL,$e = NULL, $this->wait) ){
// Conectado
$this->sock = socket_accept($this->lister);
socket_getpeername($this->sock, $raddr, $rport);
$this->user = array('ip'=>$raddr,'port'=>$rport);
return true;
} else {
// [Time out] - Não conectado a tempo.
return false;
}
}
// Fecha a conexão
public function close(){
if( is_resource($this->lister) )
socket_close($this->lister);
}
// Obtem as informações do usuário
public function getUserInfo(){
return $this->user;
}
// Le todo o sock a cada 1 Byte.
public function write($message){
$num = 0;
$length = strlen($message);
do{
$buff = substr($message, $num);
$num += socket_write($this->sock,$buff);
}while( $num != $length );
}
// Envia o ACK
public function sendAck(){
do{
$num = socket_write($this->sock,$this->ack);
}while( $num == 0 );
}
// Recebe o ACK
public function waitAck(){
do {
$buff = socket_read($this->sock,1,PHP_BINARY_READ);
}while( $buff != $this->ack );
}
// Le todo o sock a cada 1 Byte.
public function read(){
$message = '';
do {
$buff = socket_read($this->sock,1,PHP_BINARY_READ);
$message .= $buff;
}while( $buff != "\n" );
return $message;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 200.141.248.74
----
Manual Page -- http://www.php.net/manual/en/function.socket-create-listen.php
Edit -- https://master.php.net/note/edit/99259
Del: integrated -- https://master.php.net/note/delete/99259/integrated
Del: useless -- https://master.php.net/note/delete/99259/useless
Del: bad code -- https://master.php.net/note/delete/99259/bad+code
Del: spam -- https://master.php.net/note/delete/99259/spam
Del: non-english -- https://master.php.net/note/delete/99259/non-english
Del: in docs -- https://master.php.net/note/delete/99259/in+docs
Del: other reasons-- https://master.php.net/note/delete/99259
Reject -- https://master.php.net/note/reject/99259
Search -- https://master.php.net/manage/user-notes.php