OmegaNemesis Posted May 4, 2013 Share Posted May 4, 2013 (edited) 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); Edited May 4, 2013 by OmegaNemesis Quote Link to comment https://forums.phpfreaks.com/topic/277614-persistance-to-keep-a-socket-open/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.