socket_bind() function NOT binding created socket, help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

socket_bind() function NOT binding created socket, help

Post 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?
Last edited by PHPHelpNeeded on Sat Nov 22, 2014 5:12 pm, edited 2 times in total.
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

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

Post by PHPHelpNeeded »

Help please
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

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

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

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

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

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

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

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

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post 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.
PHPHelpNeeded
Forum Commoner
Posts: 83
Joined: Mon Nov 17, 2014 8:03 pm

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

Post by PHPHelpNeeded »

yeah is working now...thanks!
Post Reply