Alejandro L.
Alejandro L.

Reputation: 1076

Php socket_close() hang my client untill the server disconect me

This is my php client code:

<?php
    $service_port = 5310;
    $address = gethostbyname('192.168.40.100');

    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket === false)
        echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "<br />";
    else
    {
        $result = socket_connect($socket, $address, $service_port);
        if ($result === false)
            echo "socket_connect() falló.\nRazón: ($result) " . socket_strerror(socket_last_error($socket)) . "<br />";
        else
        {   
            $in = "Envio esto: HOLA";
            $out = 'RESPONSE: ';
            echo "ANTES: " . date("H:i:s") . "<br />"; // #1
            socket_write($socket, $in, strlen($in));
            echo "DESPUÉS: " . date("H:i:s") . "<br />"; // #2 Same time as #1
            // Leer/imprimir respuesta
            while ($out = socket_read($socket, 2048))
                echo "RESPONSE: " . date("H:i:s") . " " . $out; // #3 Same time as #2
        }
    }

    // Cerramos socket
    socket_close($socket);
    echo "BANANA: " . date("H:i:s") . "<br />"; // Always run when I got disconected from server
?>

I don't have access to the server code, but I read a little bit, it's writen in VB.net and it uses something like Client.socket.send(), I got the response and I see those echo well printed when the server disconect me, if not, my php will be hanged out in the nowhere.

EDIT I added another socket_write() in order to tell the server that, please, disconect me, but the second one is not being sent to the server.

<?php
    $service_port = 5310;
    $address = gethostbyname('192.168.40.100');

    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket === false)
        echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "<br />";
    else
    {
        $result = socket_connect($socket, $address, $service_port);
        if ($result === false)
            echo "socket_connect() falló.\nRazón: (" . $result . ") " . socket_strerror(socket_last_error($socket)) . "<br />";
        else
        {   
            $in = "Envio esto: HOLA";
            $out = '';
            $response = "RESPONSE: ";
            echo "PRIMERA;";
            socket_write($socket, $in, strlen($in));
            while ($out = socket_read($socket, 2048))
                $response .= $out;
            echo $response . ";SEGUNDA;";
            $in = "close";
            $out = '';
            $response = "";
            socket_write($socket, $in, strlen($in));
            while ($out = socket_read($socket, 2048))
                $response .= $out;
            echo $response . " " . date("H:i:s");
        }
    }
    socket_close($socket);
?>

Upvotes: 0

Views: 541

Answers (1)

crowebird
crowebird

Reputation: 2586

It could be that you are in blocking mode.

Try

socket_set_nonblock($socket);

After connecting.

This way if there is no data to read you will not hang at socket_read() and close without waiting for the server to do something.

Upvotes: 1

Related Questions