From: hickert Date: Fri, 30 Nov 2007 09:44:56 +0000 (+0000) Subject: Added socket client X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=6a3633ad21867aa9cb8ac3e1154c5b9e0fec3003;p=gosa.git Added socket client git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@7948 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/contrib/socket_server/client.php b/contrib/socket_server/client.php new file mode 100755 index 000000000..64e37babf --- /dev/null +++ b/contrib/socket_server/client.php @@ -0,0 +1,123 @@ +#!/usr/bin/php5 -q +connected()){ + echo "... successful\n"; + echo "|--Reading welcome message : \n"; + echo $sock->read(); + + /* Prepare a hunge bunch of data to be send */ + $data = "a"; + for($i = 0 ; $i < (1024 * 1024); $i++){ + $data .= "a"; + } + echo "|--Sending ".strlen($data)."bytes of data to socket.\n"; + $sock->send($data); + $sock->read(); + $sock->bytes_read(); + echo "|--".$sock->bytes_read()."bytes read.\n"; + echo "|--Sending 'exit' command to socket.\n"; + echo $sock->read(); + + +}else{ + echo "... FAILED!\n"; +} + +class Socket_Client +{ + private $host = ""; + private $port = ""; + private $timeout= ""; + private $errno = ""; + private $errstr = ""; + private $b_data_send = FALSE; + private $handle = NULL; + private $bytes_read = 0; + + public function __construct($host, $port, $connect = TRUE,$timeout = 3){ + $this->host = $host; + $this->port = $port; + $this->timeout = $timeout; + if($connect){ + $this->connect(); + } + } + + public function connected() + { + return($this->handle == TRUE); + } + + public function connect() + { + $this->handle = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout); + if(!$this->handle){ + $this->handle = NULL; + echo $this->errstr; + }else{ + $this->b_data_send = TRUE; + } + } + + public function write($data){ + return($this->send($data)); + } + + public function send($data) + { + if($this->handle){ + $data = trim($data); + fputs($this->handle,$data."\n"); + $this->b_data_send = TRUE; + return(TRUE); + }else{ + return(FALSE); + } + } + + public function close() + { + if($this->handle){ + fclose($this->handle); + } + } + + private function _read() + { + $str = FALSE; + if($this->handle){ + + /* Check if there is something to read for us */ + $read = array("0"=>$this->handle); + $num = stream_select($read,$write=NULL,$accept=NULL,1); + + /* Read data if necessary */ + while($num){ + $str.= fread($this->handle, 1024); + $read = array("0"=>$this->handle); + $num = stream_select($read,$write=NULL,$accept=NULL,1); + } + $this->bytes_read = strlen($str); + $this->b_data_send = FALSE; + } + return($str); + } + + public function read() + { + return($this->_read()); + } + + public function bytes_read() + { + return($this->bytes_read); + } +} + + +?> diff --git a/contrib/socket_server/server.php b/contrib/socket_server/server.php index bbd94dcb3..15ce8c660 100755 --- a/contrib/socket_server/server.php +++ b/contrib/socket_server/server.php @@ -67,11 +67,11 @@ while(TRUE) { ============================ Type some text here:\n"); - echo("New client connected: " . $clients[$i]['ipaddy'] . " "); + echo("New client connected: " . $clients[$i]['ipaddy'] . " \n"); break; } elseif($i == $max_clients - 1) { - echo("To many Clients connected! "); + echo("To many Clients connected! \n"); } if($ready < 1) { continue; @@ -93,13 +93,25 @@ Type some text here:\n"); /* Client disconnected */ if ($data === FALSE) { unset($clients[$i]); - echo "Client disconnected! "; + echo "Client disconnected! \n"; continue; } - /* Send some data back to the client */ - $data = base64_encode(trim($data)); - socket_write($clients[$i]['socket'],$data); + $data = trim($data); + echo "Client (".$clients[$i]['ipaddy'].") send : ".$data." \n"; + + if($data == "exit"){ + /* Close conenction */ + socket_write($clients[$i]['socket'],"Bye Bye! \n"); + echo "Client disconnected! ".$clients[$i]['ipaddy']."\n"; + unset($clients[$i]); + continue; + + }else{ + /* Send some data back to the client */ + $data = base64_encode($data); + socket_write($clients[$i]['socket'],$data); + } } } }