unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted....
error is given by my php server page. I tried different port numbers as looking from cmd as writing netstat -an. Also I searched on google but no solution. I am using wamp server and working local .
Thanks .
<?php
// don't timeout
//echo phpinfo();
set_time_limit (0);
// set some variables
$host = "127.0.0.1";
$port = 1234;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "Waiting for connections...\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "Received connection request\n";
// write a welcome message to the client
$welcome = "Roll up, roll up, to the greatest show on earth!\n? ";
socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n");
// keep looping and looking for client input
do
{
// read client input
$input = socket_read($spawn, 1024, 1) or die("Could not read input\n");
if (trim($input) != "")
{
echo "Received input: $input\n";
// if client requests session end
if (trim($input) == "END")
{
// close the child socket
// break out of loop
socket_close($spawn);
break;
}
// otherwise...
else
{
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could not write output\n");
echo "Sent output: " . trim($output) . "\n";
}
}
} while (true);
// close primary socket
socket_close($socket);
echo "Socket terminated\n";
?>
Erm...this is running on a web page? If so, each hit to the page will cause the script to try to bind to port 1234, which ain't gonna happen for any but one at a time. All the others will die.
If it's not, then there are two reasons i can think of right off why binding would fail: either another program is already using the port, or the firewall is blocking it. The latter shouldn't be the case for 127.0.0.1, but i've seen stranger things happen.
The code as posted should work, at least it does here. Are you sure there is no firewalling thing preventing you from opening the socket?
It shouldn't matter much, but when opening the socket, specify the right protocol:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
If that doesn't help, try a loop to find a listening port that may work; maybe the port is still blocked by your previous attempts.
for ( $port = 1234; $port < 65536; $port++ )
{
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
if ( $result )
{
print "bind succeeded, port=$port\n";
break;
} else {
print "Binding to port $port failed: ";
print socket_strerror(socket_last_error($socket))."\n";
}
}
if ( $port == 65536 ) die("Unable to bind socket to address\n");
If this solves your problem, you may want to do
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
before binding, to tell the system that it should allow reuse of the port.
Related
I have lots of questions about PHP sockets.
I successfully done the socket_write() part, but not socket_listen()
As my socket_bind() didn't open any port (I am not sure if bind should open port or just bind ip's, but when I write my phone IP it told can't assign requested IP, and when I tried socket_connect it returned error[111], and when I checked with netstat and telnet the port wasn't open) I decided to open port with nc
nc -kl 47832
Then I wrote this in my test.php file
so the code checks if I am with my PC(localhost) it should connect to 127.0.0.1
and if I am with phone (not localhost) it connects to 192.168.43.124 (My PCs IP)
$forloop;
if ($_SERVER["HTTP_HOST"]=="localhost") {
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Err: " . socket_last_error());
socket_connect($socket, "127.0.0.1", 47832) or die("Err: " . socket_last_error());
$result = socket_listen($socket) or die("can't set up listener");
$spawn = socket_accept($socket) or die("err: can't accept");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
$resinput = trim($input);
//echo socket_get_option();
socket_close($spawn);
socket_close($socket);
}
else {
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Err: " . socket_last_error());
socket_connect($socket, $_SERVER['HTTP_HOST'], 47832);
socket_write($socket, "needreload", 10) or die("Could not write output\n");
sleep(1);
socket_close($socket);
}
function whilefunc() {
$GLOBALS['forloop'] = 1;
if (!isset($GLOBALS['forloop'])) {
while ($GLOBALS['forloop'] == 1) {
if ($GLOBALS['input']=="needreload") {
//loadchat();
socket_successful();
}
}
}
}
function socket_successful() {
$GLOBALS['forloop']=0;
echo "<h2>msg recieved:" . $GLOBALS['input'] . "</h2>";
$resinput="";
whilefunc();
}
whilefunc();
Now when I open the page with phone in terminal text "needreload" appears, but
when I open it with PC it says can't set up listener (and that's because I didn't used bind, but if I used bind it would have told can't bind address already in use).
I changed my test.php a lot but still don't know what to do..
So my questions are
How to fix this using socket_bind() (as after I can't socket_connect, cause no port is being opened)
how to fix this (nc port opened method).
I know I wrote my question mixed, but that's because I am also confused.
For me adding this ob_implicit_flush(); to my code, made my socket accessible from telnet and nc.
I want to Run a simple Client-Server socket PHP script on my web host.
But i don't know how to exactly specify IP address and port number to work.
I test then on my wammp with success, but wen i upload them on my host control panel and run, they are not works.
Here is my codes:
Client
<?php
$host = "105.24.123.188";
$port = 8080;
$message = "hello";
echo "Message To server: ".$message. "<br>";
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
// send string to server
echo "After connect: ".$result. "<br>";
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server: ".$result. "<br>";
// close socket
socket_close($socket);
?>
Server
<?php
$host = "105.24.123.188";
$port = 8080;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
//echo "reached \n";
printf("this1\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
///echo "reached \n";
printf("this2\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message: ".$input. "<br>";
// reverse client input and send back
$output = strtoupper($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
[Error]
Client
Message To server: hello
After connect: 1
Reply From Server: HTTP/1.1 400 Bad Request
Server
can't bind
I cant find out how to run my simple example on web server.
IP address and port checked they are valid and open.
I test this on local host under wammp server in windows successfully but how i can put them in online real web?
[?]
Is anyone face this issue when Run client, server scripts in web host?
Can you guys show me some working example similar.
How to obtain valid IP, Port to test them?
I get these ip from run ping www.myhost.com on windows, is it true?
I want to execute a PHP server on my site. IP address of the site is 31.170.161.16.
The socket is not creating when i execute this code. Is this the address used for $host or any other? Please help.
<?php
// set ip and port
$host = "31.170.161.16";
$port = 4096;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
// bind socket to port
$result = socket_bind($socket, $host, $port);
// start listening for connections
$result = socket_listen($socket, 3);
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input."<br />";
// reverse client input and send back
$output = strrev($input) ."<br />";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
the host company looks like a free hosting company. just because its free that doesn't mean all the php features will be available to you. I recommend signing up with a reputable company instead such as digitalocean.com
I have a client and server program i do not have an idea of how to start the server automatically on my CENTOS server and the server should keep on running irrespective of my browser is closed, and it keeps on listening to client request. Please Help
Server.php
<?php
// set some variables
$host = "XX.XX.XXX.XX";
$port = 25763;
// don't timeout!
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
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
echo $spawn;
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Client.php
<?php
// where is the socket server?
$host = "XX.XX.XXX.XX";
$port = 25763;
$message = "Hello Server This is the first message to the server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server :".$result;
// close socket
socket_close($socket);
// print result to browser
?>
Generally, you should start .php file with exec(), for example:
exec('/usr/bin/php /var/www/directory/server/event_server.php');
Next thing, put your socket_accept() (althoug, I used different scheme, with socket_select()) in infinite loop, and dont forget about delay - this is very important, as you don't want to hang your CPU:
$broadcast_start = microtime(true);
while(TRUE) {
socket_accept($socket);
//Do stuff...
if( round(microtime(true) - $broadcast_start, 1) < 0.5 ) {
usleep(500000);
}
}
But actually, your subbjects is really complex. You should first search the web for solutions, which are plenty.
So I have this server code and it works with my client. But it gets one message from the client and sends a message back reversed.
Here is the code:
SERVER.php
<?php
$host = "127.0.0.1";
$port = 1234;
// don't timeout!
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
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
How can I edit this code so it can run continually? The client does not have to stay up of course, it will just open a new socket, send a message, get it back from server and close the socket. Next time I want to send a message, I will do the previous step again.
Now if i send a message and get a respond from server, they both close the socket.
Please help me to modify the server side so that it will not close the socket and wait for a new connection.
I tried to add a while loop but as soon the client closes, the server closes again saying that it could not read from client anymore.
Thanks
I figured it out. Most of you were close to solving it like I was by using while() loop.
But you cannot just put your code inside the while and expect it to work. The right way of doing it is as follows:
<?php
$host = "127.0.0.1";
$port = 1234;
// don't timeout!
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
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
while(true) {
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
}
// close sockets
socket_close($spawn);
socket_close($socket);
?>
If you try to put the while any other place it will introduce an error.
Thanks everyone for help :D
After the accept you need to fork - see here http://php.net/manual/en/function.pcntl-fork.php
The accept needs to be in a loop. After accept fork a process to handle that client.
i.e.
while (TRUE)
{
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
socket_close($spawn);
} else {
// we are the child
// Use $spawn for communication as you see fit
// exit();
}
}
You will need to use signal handler to tidy up zombie processes.
The "brute" way is insert your code in a infinite loop.
While(1){
.
.
}
Other way, after received your message, you can recurseively call your script.
while( TRUE ) {
// your code
}