I'm having a problem with socket read, it's only reading one character.
This may not sound so bad, but when you think that while it's reading all
those one characters, it cannot read anyone else's (it blocks).

Can anyone explain, or have a better way of doing this?

Code below:
<?php
set_time_limit(0);

define('MAXLINE', 255);
define('LISTENQ', 10);
define('PORT', 10000);

function killDaemon() {
  global $listenfd, $client;

  socket_close($listenfd);
  $msg = "Daemon going down!\n";
  for ($i = 0; $i < count($client); $i++) {
    if ($client[$i] != null) {
      socket_write($client[$i], $msg, strlen($msg));
      socket_close($client[$i]);
    }
  }
  print "Shutting down the daemon\n";
  exit();
}

function closeClient($i) {
  global $client, $remote_host, $remote_port;

  print "closing client[$i] ({$remote_host[$i]}:{$remote_port[$i]})\n";

  socket_close($client[$i]);
  $client[$i] = null;
  unset($remote_host[$i]);
  unset($remote_port[$i]);
}


$listenfd = socket_create(AF_INET, SOCK_STREAM, 0);
if ($listenfd)
  print "Listening on port " . PORT . "\n";
else
 die("AIEE -- socket died!\n");

socket_setopt($listenfd, SOL_SOCKET, SO_REUSEADDR, 1);
if (!socket_bind($listenfd, "127.0.0.1", PORT)) {
  socket_close($listenfd);
  die("AIEE -- Couldn't bind!\n");
}
socket_listen($listenfd, LISTENQ);

for ($i = 0; $i < 1; $i++) $client[$i] = null;

while(1) {
  $rfds[0] = $listenfd;

  for ($i = 0; $i < count($client); $i++) {
    if ($client[$i] != null) $rfds[$i + 1] = $client[$i];
  }

  $nready = socket_select($rfds, $null, $null, null);

  if (in_array($listenfd, $rfds)) {
    print "listenfd heard something, setting up new client\n";
    for ($i = 0; $i <= count($client); $i++) {
      if ($client[$i] == null) {
        $client[$i] = socket_accept($listenfd);
        socket_setopt($client[$i], SOL_SOCKET, SO_REUSEADDR, 1);
        socket_getpeername($client[$i], $remote_host[$i], $remote_port[$i]);
        socket_write($client[$i], "Welcome to my test server\r\n");
        print "Accepted {$remote_host[$i]}:{$remote_port[$i]} as
client[$i]\n";
        break;
      }
    }
    if (--$nready <= 0) continue;
  }

  for ($i = 0; $i < count($client); $i++) {
    if ($client[$i] == null) continue;
    if (in_array($client[$i], $rfds)) {
      $rawStr = "";
      $n = "";
      while (false !== ($n = socket_read($client[$i], MAXLINE))) {
        if (ord($n) == 0 || ord($n) == 13) break;
        if (ord($n) == 8) $rawStr = substr($rawStr, 0, -1);

        $rawStr .= $n;
      }
      if (trim($rawStr) == "") continue;

      if ($rawStr == "/killme") killDaemon();
      else if ($rawStr == "/quit") closeClient($i);
      else {
        print "From {$remote_host[$i]}:{$remote_port[$i]}, client[$i]:
$rawStr\n";
        for ($j = 0; $j < count($client); $j++) {
          if ($client[$j] != null) socket_write($client[$j], "From
client[$i]: $rawStr\r\n");
        }
        if  (--$nready <= 0)
          break;
      }
    }
  } //end of for loop

} //end of master loop
?>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to