tags:

views:

1340

answers:

3

Here is my code

<?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = 'localhost';
$port = 10000;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
do {
    $out = socket_read($msgsock, 2048);

    if (!empty($out)) {
     if ($out == 'quit') {
            break;
        }
        elseif ($out == 'shutdown') {
            socket_write($msgsock, 'plc down', 8);
      socket_close($msgsock);
            break 2;
        }
     else {
      switch ($out) {
       case "KABBE": $response = "Kabbe te!"; break;
       case "SZOPJ": $response = "Szopjal te!"; break;
       default: $response = "Ismeretlen parancs";
      }
      socket_write($msgsock, $response, strlen($response));
      break;
     }
    }
    } while (true);
socket_close($msgsock);
} while (true);

socket_close($sock);
?>

and now the errors

Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\wamp\www\socket\socket.php on line 18
socket_bind() failed: reason: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Warning: socket_listen() [function.socket-listen]: unable to listen on socket [0]: An invalid argument was supplied. in C:\wamp\www\socket\socket.php on line 22
socket_listen() failed: reason: An invalid argument was supplied.
Warning: socket_accept() [function.socket-accept]: unable to accept incoming connection [0]: An invalid argument was supplied. in C:\wamp\www\socket\socket.php on line 27
socket_accept() failed: reason: An invalid argument was supplied. 

I searched on google but nothing useful. What is the problem?

A: 

That means you already have an open socket on your computer on that port.

Try switching to another unused port.

On Windows (which is what you seem to be working on), you can see the list of open sockets from the command line:

netstat -an

If you want to know which processes are listening to those ports, try this instead:

netstat -ban
Seb
+1  A: 

'localhost' isn't a valid address as socket_bind doesn't accept DNS names, use the equivalent IP address '127.0.0.1'.

More info

+1  A: 

PHP also offers stream_socket_server and other stream_socket_* functions.
I found these to be more developer friendly.

Example code from php.net:

$socket = stream_socket_server("tcp://localhost:8000", $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)<br />\n";
} else {
  while ($conn = stream_socket_accept($socket)) {
    fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
    fclose($conn);
  }
  fclose($socket);
}
Bob Fanger