I started learning socket programming with php, i got a simple code from tutorial 1, which is the introductory example, here's the code :

<?php

$host = "127.0.0.1";
$port = 58;

set_time_limit(0);

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket");

$result = socket_bind($socket, null, $port) or die("Could not bind to socket");

$result = socket_listen($socket, 3) or die("Could not set up socket listener");

$spawn = socket_accept($socket) or die("Could not accept incoming connection");

$input = socket_read($spawn, 1024) or die("Could not read input");

$input = trim($input);

$output = strrev($input);
socket_write($spawn, $output, strlen ($output)) or die("Could not write output");

socket_close($spawn);
socket_close($socket);

?>

i am getting this error :

socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted.

and another error when i change the IP host or the port... which is similiar to this...

so what's the problem ?

    In the code you provided, you are trying to call socket_bind with a null address. I'm not certain but I don't think that will ever work.

    If you are trying to do this:

    $result = socket_bind($socket, $host, $port) or die("Could not bind to socket"); 

    and you still get an error, it could be that there is already a socket bound to that address & port. Only one socket is permitted for a given combination of protocol/host/port. Also, I've never seen an attempt to bind a socket to localhost (127.0.0.1). That may or may not cause some weird behavior.

      oops, i didn't mean null...

      "null" should be replaced by $host ( btw, i tried another host and it didn't work too... )

        It could be a number of things:
        port 58 may already have a socket attached to it (like if someone else is trying the tutorial on the same machine)
        your machine may be rejecting the host you specified for some reason
        * your system administrator may have something in place preventing sockets from being opened.

        My guess is that there is already an application using the host/port you are trying to specify. Try a port number higher than 10,000 and you'll probably have better luck. Also, maybe ask your sys admin if it is permitted to open sockets with PHP and, if so, what domains/hosts you can use.

          now i'm using the following configuration :

          $host = "192.168.1.99";
          $port = 1234;

          and the following error is what i am getting :

          Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: The requested address is not valid in its context

            Does the IP "192.168.1.99" belong to the computer on which you are trying to run this script? I tried googling around for that specific error and didn't find much but what I did find suggested that the IP you supply to the socket_bind call must be one that belongs to the computer that's running the script.

              Write a Reply...