dtqpw68806
2015-09-17 16:53 阅读 66
已采纳

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 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 . "
    ";
    
    点赞 评论 复制链接分享

相关推荐