dtqpw68806 2015-09-17 16:53
浏览 68
已采纳

PHP fputs“等待”直到脚本结束

Currently I am trying to develop a PHP script used as a publicly available part of a client/server application. The php script should be used to authenticate users with a one-time token.

The other part of the application is a java program, which offers a telnet socket for other applications to connect to. Authentication is done through this telnet connection.

The java part is already working - but I still have a huge problem with the PHP part.

In php, I have opened a connection to the telnet port of the java program, which works so far. After the connection is initialized, the java program waits for input from the PHP script in order to authenticate the user.
After the authentication process has been finished, it returns a String to the PHP script (or any other program connected to its telnet server) which the PHP script should output.

Before I explain my problem, this is the part of the PHP script where the actual communication happens:

$tnconn = fsockopen("localhost", 53135, $errno, $errstr, 2);

if(!$tnconn) {
    echo "SERVER_UNAVAILABLE";
    die();
} else {
    $data = $p_ip." ".$p_name." ".$p_token;
    fputs($tnconn, $data);

    while (true) {
        if(($telnet_response = fgets($tnconn)) == false) {
            break;
        }
    }
}

echo $telnet_response;

It seems like the fputs() statement is executed after the loop even tho it should happen before it starts - else the java application couldn't get the data that is passed to the php script, but it is needed to authenticate users.

Right after the data was received, the telnet server would output the String to indicate whether authentication was successful or not.

I tried temporarily removing the loop and the data was successfully passed with fputs() so I assume php waits until the whole script is finished and then executes the function.

How can I make it send the data before the loop?

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dpmfur2635 2015-09-17 17:17
    关注

    The issue is probably that you need to send a at the end of your data string so the telnet server knows you have sent a full sequence of data. Otherwise it is most likely sitting there waiting for more input.

    Try:

    $data = $p_ip." ".$p_name." ".$p_token . "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证