Page 1 of 1

socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:08 pm
by PHPHelpNeeded
Hi,

I already enabled the sockets extension in the php.ini file.

I got the following code source from php.net manual on sockets: http://php.net/manual/en/sockets.examples.php

The example is straight forward, so I do understand the concept, since it seems a bit similar to Java.

However, the only function call that is being performed without a problem is the socket_create(). Once the line of code creates a socket, the next function is socket_bind().

That's where the problem is, the socket_bind() function is not binding the socket.

Here is the code example (socket server program):

Code: Select all

 <?php

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '192.168.0.0';
$port = 10000;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
    /* Send instructions. */
    $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));

    do {
        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
            break 2;
        }
        if (!$buf = trim($buf)) {
            continue;
        }
        if ($buf == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.\n";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf\n";
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);

?> 
-------------------------------------

variable $address which holds the IP address of the webserver, I changed that to 'localhost' to my private ip address and even to my public ip address still the socket_bind() function still gives me this error messages:

socket_bind() failed: reason: Only one usage of each socket address (protocol/network address/port) is normally permitted.
socket_listen() failed: reason: An invalid argument was supplied.
socket_accept() failed: reason: An invalid argument was supplied.

or this error messages:

socket_bind() failed: reason: An attempt was made to access a socket in a way forbidden by its access permissions.
socket_listen() failed: reason: An invalid argument was supplied.
socket_accept() failed: reason: An invalid argument was supplied.

------------------------

Any help will be appreciated...I am trying to figure out what's wrong, why am I not able to bind the socket to establish a connection?

Re: socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:09 pm
by PHPHelpNeeded
Help please

Re: socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:14 pm
by PHPHelpNeeded
I am running IIS, and before anyone says that it is not efficient...please leave it at that, that's the only webserver I have been able to get running without a problem.

I even went to enable WebSocket protocol under adding windows features. That does not fix the problem.

Re: socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:30 pm
by Celauran
I haven't really worked with sockets, but a quick Google search suggests this may be due the port already being in use elsewhere. Are you actually using port 10000? Have you checked (say via netstat) that it isn't in use elsewhere? Does the socket_bind continue to fail on other ports? I am unable to duplicate the problem, but I am also not using Windows or IIS.

Re: socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:34 pm
by PHPHelpNeeded
I have tried other ports including 8080 but I still get the same two error messages.

I don't know how to figure out how to check whether a port is already being used.

Re: socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:42 pm
by Celauran
PHPHelpNeeded wrote:I don't know how to figure out how to check whether a port is already being used.

Code: Select all

netstat -an

Re: socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:44 pm
by PHPHelpNeeded
I used netstat -a to see all active TCP/UDP ports, and I don't see port 8080 or 19999 being used, but I still get this error:

Only one usage of each socket address (protocol/network address/port) is normally permitted

Re: socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:52 pm
by Celauran
This seems to be Windows-specific, rather than PHP-specific, so I'm going to leave this for someone more familiar with Windows/IIS. Sorry I couldn't be of more help.

Re: socket_bind() function NOT binding created socket, help

Posted: Sat Nov 22, 2014 5:57 pm
by PHPHelpNeeded
At least you tried, thanks!

But I wish php would be bit more straight forward. The problem here with the example I got from the php.net manual is that they are assuming your system is ready. And my problem as anyone can see, is figuring out whether my system is ready... and as it seems, my system is NOT READY since I cannot run that code example. Books only tell you to enable the sockets dll extension, which I already mentioned to have done...and I ran the phpinfo() to see whether it work, and it does says, sockets enabled.

So I am still lost as to way it is not working.

Re: socket_bind() function NOT binding created socket, help

Posted: Sun Nov 23, 2014 7:23 pm
by Weirdan
Try using a port number between 49152 and 65535. MSDN says it's a dynamic client port range, so you have better chances to avoid firewall or access permissions problems with it.

Re: socket_bind() function NOT binding created socket, help

Posted: Mon Nov 24, 2014 5:02 am
by PHPHelpNeeded
yeah is working now...thanks!