0

This Is my code to connect java socket :-

$socket         = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, '127.0.0.1', 12345);

while(true) 
{
    // read a line from the socket
    $line = socket_read($socket, 1024, PHP_NORMAL_READ);

    var_dump($line);

    $someArray  = json_decode($line, true);
    $otp         = $someArray["otp"];

    if($someArray["msg"] == "otp_generation")
    {           
        $myObj      = new \stdClass();          
        $myObj->msg = "OTP RECEIVED NEED TO CONNECT";       
        $send       = json_encode($myObj);

        socket_send($socket, $send, strlen($send), 0);
    }
    exit;       

}

My Question is -

When connection is established successfully server send one OTP to client and received successfully in client. Then i send data to server OTP RECEIVED acknowledgement, it also received in server. After OTP RECEIVED acknowledgement server send welcome msg to client. I cant get the welcome message. if i remove the "exit" code browser is still loading, finally crashing. Why i didn't receive the second data. anyone solve my issue. what i need to modify. am beginner for socket.

I need to display Welcome msg. What can i do?

4
  • 2
    When you remove exit you have an infinite loop since it doesn't check for a condition to break out. Commented Apr 3, 2020 at 21:19
  • 1
    Does the server close the connection after sending the welcome message? $line will be FALSE when the server closes the connection, you should check for that. Commented Apr 3, 2020 at 21:20
  • @Barmar server didn't close the connection. It will close after 3 minutes. Commented Apr 4, 2020 at 4:51
  • @Barmar if i remove the exit, it will infinite but i cant trace any data from server. If i put exist 1st response will show. Could u tell what can i do. Any examples you have Commented Apr 4, 2020 at 4:52

2 Answers 2

1

You need to continue looping and read the next message, then break out of the loop.

while(true) 
{
    // read a line from the socket
    $line = socket_read($socket, 1024, PHP_NORMAL_READ);

    var_dump($line);

    $someArray  = json_decode($line, true);

    if($someArray["msg"] == "otp_generation")
    {           
        $otp         = $someArray["otp"];
        $myObj      = new \stdClass();          
        $myObj->msg = "OTP RECEIVED NEED TO CONNECT";       
        $send       = json_encode($myObj);

        socket_send($socket, $send, strlen($send), 0);
    } elseif ($someArray["msg"] == "welcome") {
        // do whatever you need to do with the rest of the message
        break; // then get out of the loop
    } else {
        echo "Unknown message received";
        var_dump($someArray);
        break;
    }
}

I had to make a guess about how the welcome message is formatted, but this should give you the general idea.

Sign up to request clarification or add additional context in comments.

15 Comments

Thanks for your reply. Browser is still loading, output is not displayed. could u suggest any other option there for tcp simultaneous connection.
Are you sure the server is sending a second message? It sounds like the second call to socket_read() is waiting for the message.
yes i develop the server also. it send the second message. Could you find the solution Please.
If i put exit "OTP RECEIVED NEED TO CONNECT" message is send to server otherwise is not send.
I've added an else clause to handle an erroneous message
|
0

Without new line cmd data is not send. This is the mistake i done. Finally i got the answer from my friend.

I just add the line below;-

socket_send($socket, $send."\r\n", strlen($send."\r\n"), 0);

Thanks @hemanth kumar and @Barmar

Comments

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.