PHP Socket stucking in accept - php

I have a php socket server and a javascript websocket, but websocket stuck in connecting to socket.
There is no error but websocket stay connecting.
sock.php
set_time_limit(0);
$ip = '127.0.0.1';
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$ret = socket_bind($sock, $ip);
$ret = socket_listen($sock);
do
{
$ref = socket_accept($sock);
/*
Write message
*/
$msg ="Success receive from client\n";
socket_write($ref, $msg, strlen($msg));
/*
Read message
*/
$buf = socket_read($ref , 1024);
echo "Received message: $buf\n";
socket_close($ref);
}while (true);
socket_close($sock);
?>
javascript websocket:
var sock = new WebSocket('ws://localhost/sock.php');
sock.onopen(function()
{
console.log('socket connected.');
});

You must specify the correct port in socket_bind() or it will listen on a random port. You check this with netstat on Linux and Windows. As you try to connect to ws://localhost that should be port 80. However binding to port 80 is usually not allowed, unless you are an admin user. Best to bind to another port, for example 8080, and connect to ws://localhost:8080.

Related

php client server on web host cant bind

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?

how to implement socket connection on server?

I am initiator of socket programming. I am trying to implement the socket connection.
But I don't know which IP and port should I use for server.
The code I took from a link :
server.php:
<?php
// set some variables
$host = "<?>";
$port = 5555;
// don't timeout!
set_time_limit(0);
// create socket
//echo 'hello';
$socket = socket_create(AF_INET, SOCK_STREAM, 0) 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;
}
// 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 'hello';
// 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 = $_REQUEST['server_input'];
$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);
?>
- client.php
<?php
//include_once('server.php');
$host = "<?>";
$port = 5555;
$message = "hi from client";
//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);
?>
Description :
This function accepts incoming connection request on the created socket. After accepting the connection from client socket, this function returns another socket resource that is actually responsible for communication with the corresponding client socket. Here “$spawn” is that socket resource which is responsible for communication with client socket.
But I don't know which IP and port should I use for server.
The IP for use is same static IP set on your server's network, If you try to local test, Can use 127.0.0.1 for IP.
And also, for port, you can use one desired number between 1024 to 65535.
Note that, the IP and Port used on the server and client must be same.

PHP Socket Programming : Server not accessible from outside network

I am working on PHP socket programming project. In this project we are going to create a service in php socket. This socket will listen on one particular port. And client from outside network will able to communicate on that port.
Till now I am able to create server and client in php for socket programming. Now my pc is connected to LAN so I have to use port forward for connecting my pc with outside client. I forward port 2000 and all communication on that port is transfer to my pc IP address. I have netgear router n150 wireless adsl .I add all configuration on that router. I test port forwarding online at this site http://www.yougetsignal.com/tools/open-ports/ it says port is open.
I test my code on locally (intranet), it is working fine. But when I trying to run server on my pc and client from web server which is my ipage hosting server. It throws me error "Server Could not connect to server".
Server.php
<?php
// set some variables
// My LAN Ip
$host = "192.168.0.5";
$port = 2000;
// 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");
$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";
socket_write($spawn, $output, strlen($output)) or die("Could not write output\n");
// close sockets
socket_close($spawn);
socket_close($socket);
?>
Client.php
<?php
//my public ip
$host = "117.223.90.191";
// port on which I port forword
$port = 2000;
$message = "Hello 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);
?>
Any suggestion for problem. I think many will have same problem like me.
Even if i think the problem is in the lan forwarding, try testing it with :
telnet 117.223.90.191 2000
another thing to try is to make the server listen on all interfaces
$host = "0.0.0.0";
and take a look at http://reactphp.org/

PHP socket_bind error (only one usage of socket address)

Below is a PHP script that I have created to listen for incoming messages (XML strings).
That PHP script is hosted on my local home server on port 13330 so that's where I would listen for incoming requests, right? So I create the socket and bind it to the address the file is located on.
I receive this error: Warning: socket_bind(): unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted.
I would appreciate it if anyone could let me know why I might be seeing that.
Thanks
createSocketServer();
function createSocketServer() {
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 13330;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
echo '<p>Socket created</p>';
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
echo '<p>Socket binded</p>';
// Start listening for connections
socket_listen($sock);
echo '<p>Socket listening</p>';
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024 bytes
$input = socket_read($client, 1024);
// Strip all white spaces from input
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
echo $output;
}
use this code before binding:
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}
for reference http://www.php.net/manual/en/function.socket-bind.php
You can also check http://www.php.net/manual/en/function.socket-set-option.php for details

unable to bind address [0]: php error

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.

Categories