Jump to content

Persistance To Keep A Socket Open


Recommended Posts

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;\


// 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

Edited by OmegaNemesis
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.