3

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);
user99999991
  • 1,351
  • 3
  • 19
  • 43
  • 3
    PHP will always close the socket after handling a HTTP request. What you're trying to do will likely require running an instance of PHP-CLI in the background in a loop in order to keep the socket open. The server-side script can then communicate with the CLI script over a socket. Really complicated setup. – cleong May 04 '13 at 05:39
  • So if we receive data, try to pass this data off to another script before taking another response, there is no way around closing the socket without complicated stuff? – user99999991 May 04 '13 at 05:43
  • 1
    No standard way to do what you're trying to do. Something like Gearman might be useful here. Beats doing it from scratch. – cleong May 04 '13 at 05:49
  • what about pfsockopen and with session saving? I'm reading that there are ways to get it to persist into different pages, but I guess I'm just having trouble finding a way to implement it properly [if it at all]. Does that also hold true to what you're saying? – user99999991 May 04 '13 at 06:20
  • I've never used pfsockopen() so I don't know. Persistent connections in PHP typically just save you the cost of making the connection by reusing an earlier open connection. There's usually no guarantee that consecutive requests would end up getting the same connection. – cleong May 04 '13 at 06:45
  • What about using node.js and socket.io?, they work great and you can get access to MySQL, filesystem, and so on. Btw, is server - server or server - browser? – coma May 04 '13 at 09:33
  • We looked at node.js briefly but went with c# just cause it worked instantly now. We're not too familiar with node.js. We were going for browser (client) -> <- Server. – user99999991 May 04 '13 at 19:01

0 Answers0