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 . "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料