this is my first post to the forum so apologies for any newbie mistakes...
I have a php script the opens a streaming socket to the server, sends a request and waits for the response message(s). The script looks like this:
<?php
$addr="192.168.1.1";
$port=6010;
$sock = fsockopen("tcp://$addr:$port", $errno, $errstr, $timeout) or die("socket problem");
stream_set_timeout($sock,30) or die("timeout not set");
$message='<?xml version="1.0" encoding="UTF-8"?><Request><Type>1</Type></Request>'
$slen=pack("N",strlen($message));
fwrite($sock,$slen.$message);
ob_flush();flush();
while((!feof($sock)) && (!$info['timed_out'])){
$b3=fread($sock,3);
$b4=fread($sock,1);
$temp=unpack("N",$b3.$b4);
$length=$temp[1];
$lines=fread($sock,$length);
if($lines){
if(preg_match("'<Response>(.*?)</Response>'",$lines, $match)){
if($match){
$content=$match[1];
echo nl2br($content);
}
}
}
$info = stream_get_meta_data($sock);
}
fclose($sock);
?>
Above script works well, but I would like to add there functionality (a cancel -button) to exit the while loop and send cancellation message to the server using the same socket, e.g. Type 0
The problem is that submit will refresh the page and loose current $sock.
Any ideas or simple solution would be highly appreciated.
Thanks. T.