Por67
Por67

Reputation: 11

Error using socket_bind() on a shared hosting provider

I have a server service script and client service script written in PHP using PHP's sockets API. They are working fine in localhost, but after uploading those two scripts to my hosting account on ipage, the server script is giving me an error on the socket_bind call. Here is the code I am using on the server:

$host = "my_own_hosting"; 
$port = 25003; 
$message = "Hello Client"; 
set_time_limit(0); 
// create socket 
$socket = socket_create(AF_INET, SOCK_STREAM, 0) 
    or die("Could not create socket\n"); 
// bind socket to port (this is the call that returns the error)
$result = socket_bind($socket, $host, $port) 
    or die("Could not bind to socket\n");
// put server into passive state and listen for connections 
$result = socket_listen($socket, 3) 
    or die("Could not set up socket listener\n");

Any ideas on why this call is not working?

Upvotes: 0

Views: 803

Answers (1)

NotGaeL
NotGaeL

Reputation: 8484

First and foremost:

Use socket_strerror and socket_last_error to figure out your problem:

if (!($result = socket_bind($socket, $host, $port)){ 
    die(socket_strerror(socket_last_error($socket)));
}

You are trying to run your server script on a shared PHP hosting provider (ipage).

These kind of providers do not generally allow to use the PHP sockets interface to build your own services, or implement harsh restrictions (Such as allowed ports or protocols for running the service) to avoid abuse and prevent safety issues. Either you comply with them or change your hosting service to one that fits your needs.

It seems your provider allows using the sockets interface though.

(You can check it out by searching for socket on http://www.hostingreviewbox.com/features/ipage-php/ although it's best if you make sure of it by running phpinfo on your account yourself, or even better: Open up a support ticket with your hosting provider and ask them if sockets are in any way restricted on your account.)

Then you should make sure of 2 things:

  1. Are you using the proper ip address on $host?

You can ping my_own_hosting and see what you get or even better, add this code snippet to your server script for figuring out your server's public ip at runtime:

  $host = my_ip();
  echo "my ip address is $host\n";

  function my_ip($dest='8.8.8.8', $port=53)
  {
    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    socket_connect($socket, $dest, $port);
    socket_getsockname($socket, $addr, $port);
    socket_close($socket);
    return $addr;
  }

(from http://php.net/manual/en/function.socket-create.php#49368)

  1. Is the port you are trying to use already in use or restricted?

Try to use a different port or ask your hosting provider about it.

On a side note:

Trust the PHP manual before its comments: You should use getprotobyname or SOL_TCP for determining the third parameter of socket_create instead of just sending 0.

Lastly, you should consider using socket_create_listen instead of socket_create + socket_bind + socket_listen. Or, for this particular case, stream_socket_server and stream_socket_accept. Like the PHP manual shows, they are way more practical:

$socket = stream_socket_server("tcp://{$host}:{$port}", $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);
}

Upvotes: 1

Related Questions