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?