chrischabot / phpsocketdaemon

Automatically exported from code.google.com/p/phpsocketdaemon
GNU Lesser General Public License v2.1
0 stars 1 forks source link

we have an errors #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Fatal error: Uncaught exception 'socketException' with message 'Could not
bind socket to [0 - 2001]: Address already in use' in
/home/www/site/socket.php5:56 Stack trace: #0
/home/www/site/socketServer.php5(27): socket->__construct(0, 2001, 2, 1, 6)
#1 /home/www/site/socketDaemon.php5(28):
socketServer->__construct('httpdServerClie...', 0, 2001) #2
/home/www/site/httpd.php5(32): socketDaemon->create_server('httpdServer',
'httpdServerClie...', 0, 2001) #3 {main} thrown in
/home/www/site/socket.php5 on line 56

how can we get it to work?????

#!/usr/bin/php -Cq
<?
ini_set('mbstring.func_overload', '0');
ini_set('output_handler', '');
error_reporting(E_ALL |  E_STRICT);
@ob_end_flush();
//set_time_limit(0);
include("socket.php5");
include("httpServer.php5");

$daemon = new socketDaemon();
$server =  $daemon->create_server('httpdServer', 'httpdServerClient', 0, 2001);
$daemon->process();

$client = $daemon->create_client('myClientClass', 'mysite.com', 80);

      class myClientClass extends socketClient {
      public function on_read()
      {
      // handle incomming data which is in $this->read_buffer, forexample the
      // following looks for \r\n, then parses the complete instruction to
'handle_request'
      while (($pos = strpos($this->read_buffer,"\r\n")) !== FALSE) {
      $string            = trim(substr($this->read_buffer, 0, $pos + 2));
      $this->read_buffer = substr($this->read_buffer, $pos + 2);
      $this->handle_request($string);
      }
      }
      // other event hooks that exist:
      public function on_connect() { echo "contected"; }
      public function on_disconnect() { echo "disconected";}
      //public function on_read() {}
      public function on_write() {}
      public function on_timer() {}
      }

Original issue reported on code.google.com by andrew.w...@gmail.com on 16 Nov 2007 at 10:31

GoogleCodeExporter commented 9 years ago
Hi;
this issue is common when you try to bind a socket to an address&port that is 
already
used by an other socket, and the last one does not permit to reuse them; to 
solve it
you should set the option "SO_REUSEADDR" to "true", wich is already set in the
"socketServer" class in its __construct method.
Now i'll tell why it still to happen...

in the exapmle "httpserver" class implemented in "httpServer.php5" file is a 
subclass
of "serverSocket" class; but you missed to call the parent::__construct(...) 
method
in the class constructor; and as you should know php (unlike c++) does not 
invoke the
parent's constructor implicitly.
that's it; hoping that it helped.

Original comment by drsl...@gmail.com on 8 Dec 2007 at 11:05