0

I have a PHP file talking to a Java socket server, and when I send data over, my java server gets stuck (hung, frozen) on inputLine = in.readLine(). I've debugged and found that it's only when I read data, this happens.

Here's my java method for the server:

public void start_echo_server(int port){
    main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "STARTING SOCKET LISTENER (echo)"));

    int portNumber = port;
    try {
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();
        // accepted the connection
        main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "ACCEPTED"));
        // in stream
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        // outstream
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

        String inputLine;
        StringBuilder sb = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine);
        }
        String final_line = sb.toString();
        main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "IN: " + final_line));
        //String final_ret = parser.parse_message(final_line);
        //main.getProxy().getConsole().sendMessage(new TextComponent(ChatColor.GOLD + "FINAL: " + final_ret));
        out.println(final_line);
        in.close();
        out.close();
        serverSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And here's my PHP file:

    <?php
  if( isset($_POST['username']) )
  {
     $username = $_POST['username'];
     parse($username);
  }else{
    echo "Missing parameters!";
    exit();
  }
  function parse($username){
    //Must be same with server
    $host = "127.0.0.1";
    $port = 59090;
    // No Timeout
    //Create Socket
    $sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
    //Connect to the server
    $result = socket_connect($sock, $host, $port) or die("Could not connect toserver\n");
    $message = "player_online ". $username;
    //Write to server socket
    $len = strlen($message);

    socket_write($sock, $message, $len) or die("SENDING ERROR ". $message ." \n");
    //Read server respond message
    $result = socket_read($sock, 1024) or die("RESPONSE ERROR ". $message ." \n");
    echo "Reply From Server  :".$result;
    //Close the socket
    socket_close($sock);
  }
?>

The problem is when I do socket_write (writing the data) on the PHP side, but the issue is at the java line while ((inputLine = in.readLine()) != null) {.

Thanks so much!

1
  • Readline reads a "line", which is a sequence of characters ending in any of three different newline sequences. Is your PHP client sending a newline character? It doesn't look like it. Commented Jul 23, 2019 at 23:59

2 Answers 2

2

Solved!

I was reading multiple lines with only one line coming in, while I didn't include a newline (\n) after the message (which signifies that the previous message was a line that is finished). Replace PHP $message = "player_online ". $username; with $message = "player_online ". $username ."\n";

Also had to replace Java

String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
    sb.append(inputLine);
}
String final_line = sb.toString();

with

String inputLine = in.readLine();
String final_line = inputLine;
Sign up to request clarification or add additional context in comments.

Comments

0

Try changing the while condition to something you control as a proof of concept i.e read during 1 minute or so, if that unstucks ypu then do nor read while in.readline but fimd something else, it happened to me on some ssh connection, we then set the while condition to read while channel is open...will try to find that code and add it here if you dont get to somerhing based on the above proof of concept

4 Comments

1) I have a hard time understanding your response because of misspellings 2) I tried this, and I still get the same result while reading from inside the loop on a controlled condition
My bad, Im writing on my phone, didnt realize...will try to get the code tomorrow, it is for an ssh connection but had the same issue, apologies
You're fine, I didn't realize you were on your phone. Will that be applicable to my situation? (ssh v.s. sockets)
Not completely sure but, ssh opens a socket in the end, I remember we got that from JCabi ssh connection classes as we had the exact same issue, I will try that by tomorrow I hope someone answers you before so you stop struggling with this issie as it sucks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.