1

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.

3
  • Do you need to send a CR or CRLF at the end of $data to signal the telnet server you have sent a complete sequence of data? Otherwise its probably sitting there waiting for more input and doesn't send anything back? Like $data = $p_ip." ".$p_name." ".$p_token . "\n";
    – drew010
    Sep 17, 2015 at 16:58
  • @drew010 Thank you a lot, this was the issue indeed. I did not realize this at all because I am pretty much used to java's println doing this automatically.
    – Michael G.
    Sep 17, 2015 at 17:05
  • Glad that was it, I'll add it as an answer and if you don't mind accepting it I'd appreciate it.
    – drew010
    Sep 17, 2015 at 17:14

1 Answer 1

0

The issue is probably that you need to send a \n 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 . "\n";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.