duangou2028 2017-08-13 22:15
浏览 40

使用现有的PHP套接字

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.

  • 写回答

1条回答 默认 最新

  • dongshi1934 2017-08-13 23:41
    关注

    Elaborating on what @Scuzzy suggests, one option can be to have the "Cancel Button" calling asynchronously to another php script. This would set a flag on file/disk/database (your choice) indicating that user asked to Cancel.

    Async call will preserve the session on the main php script.

    In the main loop here, you periodically check for that flag status, and exit immediately if the flag is set, performing additional cleanup if needed.

    The pseudo-code for the main loop would be:

    <?php
    /*
       All the initialization
    */
    
    cancelOperation = False;
    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);
    
        // Now check external condition, from either file or db for example
        cancelOperation = checkExternalCondition();
        if (cancelOperation === True) {
            break;
        }
    }
    if (cancelOperation === True) {
        // additional cleanup needed goes here.
    }
    fclose($sock);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?