Problem with creating PHP based socket server

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Problem with creating PHP based socket server

Postby bijanv » 12. June 2008 19:11

Hey everyone,

I just installed XAMPP and everything is going fine however I created my own PHP socket server for an application I'm writing and I keep getting the following error:

Code: Select all
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.



This is my code

Code: Select all
<?php

// Set time limit to indefinite execution
set_time_limit (0);

// Set the ip and port we will listen on
$address = 'xxx.xxx.xxx.xxx';
$port = xxxx;
$max_clients = 10;

// Array that will hold reader information
$client = array();

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die(mysql_error());
// Start listening for connections
socket_listen($sock);

echo 'Just before while(true) loop';

// Loop continuously
while (true) {
    // Setup clients listen socket for reading
    $read[0] = $sock;
    for ($i = 0; $i < $max_clients; $i++)
    {
        if ($client[$i]['sock']  != null)
            $read[$i + 1] = $client[$i]['sock'] ;
    }
   
   // Set up a blocking call to socket_select()
   $ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
    echo 'Checking for new connection';
   /* if a new connection is being made add it to the client array */
    if (in_array($sock, $read)) {
        for ($i = 0; $i < $max_clients; $i++)
        {
            if ($client[$i]['sock'] == null) {
                $client[$i]['sock'] = socket_accept($sock);
                echo 'Added connection to client array';
          break;
            }
            elseif ($i == $max_clients - 1)
                print ("too many clients");
        }
        if (--$ready <= 0)
            continue;
    }
   
   // If a client is trying to write - handle it now
    for ($i = 0; $i < $max_clients; $i++) // for each client
    { echo 'client trying to write';
        if (in_array($client[$i]['sock'] , $read))
        {
            $input = socket_read($client[$i]['sock'] , 1024);
            if ($input == null) {
                // Zero length string meaning disconnected
                unset($client[$i]);
            }
            $n = trim($input);
            if ($input == 'exit') {
                // requested disconnect
                socket_close($client[$i]['sock']);
            } elseif ($input) {
                // strip white spaces and write back to user
          echo 'Supposed to write back here';
                $output = ereg_replace("[ \t\n\r]","",$input).chr(0);
                socket_write($client[$i]['sock'],$output);
          //echo $output."<br/><br/>";
            }
        } else {
            // Close the socket
            if ($client[$i]['sock'] != null){
            socket_close($client[$i]['sock']);
            unset($client[$i]);
         }
        }
    }
} // end while
// Close the master sockets
socket_close($sock);
?>



Can anyone tell me why this is happening? Is it because Apache is using the ports? How can I make Apache not use the ports I want to use for this application?
bijanv
 
Posts: 3
Joined: 12. June 2008 19:06

Postby bijanv » 13. June 2008 00:01

actually it turns out I'm not supposed to be running this file on the web server.. instead I'm supposed to be using this through the command line in the PHP binary:

$ /usr/local/bin/php -q socket_server.php


Any idea of how I'm supposed to issue that command?
bijanv
 
Posts: 3
Joined: 12. June 2008 19:06

Postby Dave_L » 13. June 2008 13:49

Have you tried this from a command prompt?

C:\> C:\xampp\php\php.exe -q socket_server.php

(That's assuming you installed XAMPP in C:\xampp.)
User avatar
Dave_L
 
Posts: 212
Joined: 23. October 2004 00:43

Postby bijanv » 13. June 2008 23:25

ah perfect! I can't believe I didn't realize that from the code. But thanks a lot everything's working fine now!
bijanv
 
Posts: 3
Joined: 12. June 2008 19:06


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 122 guests