Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket_bind problem. why?

Status
Not open for further replies.

djwiktor

Vendor
Jul 8, 2005
2
PL
I have a problem with a simple (allegedly) function to read socket data and save it in a file. The code look as follows:

Code:
// Set the ip and port we will listen on 
$host = '127.0.0.1'; 
$port = 20000; 

set_time_limit(0);

// create low level socket
if(!$socket=socket_create(AF_INET,SOCK_STREAM,0)){
    trigger_error('Error creating new socket',E_USER_ERROR);
}

# reuse socket tweak
  if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
    echo socket_strerror(socket_last_error($socket));
    exit;
  }

// tie up socket to TCP port
if(!socket_bind($socket,$host,$port)){
    trigger_error('Error binding socket to TCP host:'.$host.', port:'.$port.'',E_USER_ERROR);
}

// begin listening connections
if(!socket_listen($socket)){
    trigger_error('Error listening socket connections',E_USER_ERROR);
}

// create communication socket
if(!$comSocket=socket_accept($socket)){
    trigger_error('Error creating communication socket',E_USER_ERROR);
}

// read socket input
$socketInput=socket_read($comSocket,1024);

//write data to file
....

// close sockets
socket_close($comSocket);

socket_close($socket);

However, right away after the script is launched I get an error message

Code:
PHP Warning:  set_time_limit(): Cannot set time limit 
PHP Warning:  socket_bind() unable to bind address [98]: Address already in use Could not bind to address

This problem occurrs regardless of port number and if it is declared in httpd.conf or not. No matter which server port I use it always seems 'busy'.

Please give me a hint what is the cause of the problem.

Wiktor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top