I am trying to connect to a server socket which will send me a bunch of data after connecting, take a response from me, and then send a bunch more data, repeating this process until it determines its had enough.
So basically, after first~ connecting, we will (and currently are) receiving data from the server. We want to take this data, compute it in another script/program passing with AJAX, and then return to this and respond to the server.
We're afraid that once we take data from the server, go to compute the data, the socket is going to close and we're not going to be able to continue where we left off.
How can we make sure that php persists in its connection to this socket? I've looked into fsockopen and I'm not quite understanding of it and whether it will help here or not. Any assistance?
// create socket
//$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$socket = fsockopen($host, $port, $errno, $errstr, 30);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
}
$_SESSION['socket'] = $socket;
// receive DATA from server
//$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
echo "Connected to server";
//$_SESSION['connection'] = $result;\
//STOP, PASS DATA, COMPUTE, SEND RESPONSE
// send response to server
fwrite($socket, $message1) or die("Could not send data to server\n");
// get data server response
$result = fread ($socket, 1024) or die("Could not read server response\n");
echo "<br>Reply From Server :".$result;
// close socket
fclose($socket);
Related
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 get the sample code for PHP socket client and try out.
Is there a way to continue loop socket_read and pass the receive message to a local variable or even session continuously, so another php can show it?
while (true) {
$out = socket_read($socket, 2048);
echo $out;
}
thanks
you create the session and connect socket. Then, read socket and save value in session.
session_start();
// 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");
while (true) {
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
$_SESSION["result"]=$result;
}
If we create a socket in server side then it runs in a infinite loop, cant we do something like this for the client side? Can we create a listening mood in a infinite loop? Do I need to create a new socket every for this every time?
Here is my code, it writes only once, when I try to write after socket_read it doesn't work.
Server side code
<?php
$host = "192.168.56.1";
$port = 8080;
$message = "Hello Client";
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket) or die("Could not set up socket listener\n");
echo 'listining ip '.$host." at port ".$port;
while(true){
$com = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($com, 1024) or die("Could not read input\n");
$input = trim($input);
echo '
Client says: '.$input;
socket_write($com, $message , strlen ($message)) or die("Could not write output\n");
}
echo '
server closed';
socket_close($com);
socket_close($socket);
?>
client side code
<?php
$host = "192.168.56.1";
$port = 8080;
$message = "Hello Server side";
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die ("Could not connect to server\n");
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$result = socket_read($socket, 1024) or die("Could not read server response\n");
socket_write($socket,$message, strlen($message)) or die("Could not send data to server\n");
echo "Server says :";//.$result;
socket_close($socket);
?>
If we create a socket in server side then it runs in a infinite loop,
cant we do something like this for the client side? Can we create a
listening mood in a infinite loop?
Of course - if your server and client would send and receive strictly in turn, you could simply change the socket_read line in the client to
while ($result = socket_read($socket, 1024))
Do I need to create a new socket every for this every time?
No, you must not, since it wouldn't be the same connection.
But for a more complete server example which handles multiple connections and disconnections, cf. this answer to How to write server for chat program in php by using socket.
I have created a client and server programs using socket it is working perfectly on the localhost but when i upload it to the server through cpanel it does not work properly and it gives following error. The main thing is some times both of the program works on the server.
I am new to socket programming and seen some of the related posts on stackoverflow but was unable to found exact solution.
Client: Warning: socket_connect(): unable to connect [111]: Connection refused in .../client.php on line 10
Could not connect to server
Server:Warning: socket_bind(): unable to bind address [98]: Address already in use in ..server.php on line 10
Could not bind to 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
?>
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);
?>
For the server you might like to set option SO_REUSEADDR on the freshly created socket using socket_set_opt().
For code how to do this please see the example section on manual page linked above.
I am using the following php code to connect and send a request to a remote server through php sockets connection.
<?php
$host = "X.X.X.X";
$port = 1234;
$message = "<request>test</request>";
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
stream_set_timeout($socket, 10);
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
//echo "Reply From Server :".$result;
$info = stream_get_meta_data($socket);
if (isset($info['timed_out']) && $info['timed_out'])
{
echo 'Connection timed out!';
}
else
{
var_dump($result);
}
// close socket
socket_close($socket);
?>
As confirmed, the remote machine is receiving our request and sending appropriate response to us over the same session. But unfortunately, I am getting the following response when the script is executed -
504 Gateway Time-out. The server didn't respond in time.
When tried from Putty (same server from where the code was executed), I am getting the response almost instantly (in less than a second).
There is no firewall running that would block the responses. All VPN traffic passes through the tunnel without any blocks as seen in telnet request and response.
Now I am unable to figure out the exact reason of not getting the response through this php script. I had gone through many tutorials and sample codes but found no luck. :(
Members, please help.
I've worked with php socket that simulates a telnet session without setting a timeout parameter. Try the below and let me know how it well it goes
<?php
// You can use socket_strerror(socket_last_error()) to get the error details
$socket = socket_create(AF_INET,SOCK_STREAM,0) or die ('Could not create socket') ;
$result = socket_connect($socket,$host,$port) or die ('Could not connect to host');
// The server expects the enter key to pressed to execute the command which i simulate by
// appending a \n to the message sent to the server
socket_send($socket,$message,strlen($message),0) or die('Could not execute command');
$result = socket_read($socket,2048) or die('Could not read from socket');
var_dump($result);
This might be outdated, but a note on The PHP manual says:
In case anyone is puzzled, stream_set_timeout DOES NOT work for sockets created with socket_create or socket_accept. Use socket_set_option instead.
Instead of:
<?php
stream_set_timeout($socket,$sec,$usec);
?>
Use:
<?php
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>$sec, 'usec'=>$usec));
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>$sec, 'usec'=>$usec));
?>
Try setting the read mode in socket_read to PHP_NORMAL_READ