Code

Added socket client
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 30 Nov 2007 09:44:56 +0000 (09:44 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 30 Nov 2007 09:44:56 +0000 (09:44 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@7948 594d385d-05f5-0310-b6e9-bd551577e9d8

contrib/socket_server/client.php [new file with mode: 0755]
contrib/socket_server/server.php

diff --git a/contrib/socket_server/client.php b/contrib/socket_server/client.php
new file mode 100755 (executable)
index 0000000..64e37ba
--- /dev/null
@@ -0,0 +1,123 @@
+#!/usr/bin/php5 -q
+<?php
+
+error_reporting(E_ALL);
+
+echo "\n\nTry to connect";
+$sock = new Socket_Client("localhost","10000");
+if($sock->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);
+       }
+}
+
+
+?>
index bbd94dcb3b53d173676da0fc3533e1c79c510134..15ce8c660d31deb443b16b32656ea3b01b45f5e2 100755 (executable)
@@ -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);
+                       }
                }
        }
 }