socket_bind() function NOT binding created socket, help
Posted: Sat Nov 22, 2014 5:08 pm
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):
-------------------------------------
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?
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?