Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi there's problem with the socket_read function below, it does not return empty string '' after it has finished reading the data. On second call to socket_read() it just hang there, the page keep loading in browser with no return. It works if the server close the connection, is it a must for server to close the client socket ? Thanks

    $json = $_GET['json'];

    /* Get the port for the WWW service. */
    // $service_port = getservbyname('www', 'tcp');
    $service_port = "1111";

    /* Get the IP address for the target host. */
    $address = "192.168.3.5";

    /* Create a TCP/IP socket. */
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
        die();
    } else {
        //echo "OK.\n";
    }

    //echo "Attempting to connect to '$address' on port '$service_port'...";
    $result = socket_connect($socket, $address, $service_port);
    if ($result === false) {
        echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
        die();
    } else {
        //echo "OK.\n";
    }

    $out = '';

    //echo "Sending HTTP HEAD request...";
    $result = socket_write($socket, $json, strlen($json));

    if ($result === false) {
        echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
        die();
    } else {
        //echo "OK.\n";
    }



    echo socket_read($socket, 2048);
    echo socket_read($socket, 2048);
//  
     // while (socket_read($socket, 2048) !== '') {
        // echo $out;
//       
     // }
//   
     socket_close($socket);
share|improve this question
add comment

1 Answer

Yes, that's exactly what I'd expect it to do - socket_read() will never return an empty string unless you open it a binary and the server responds with '\0'.

You seem to be trying to write an HTTP client. If you are, STOP NOW, throw away your code and go read up on the Curl extension. HTTP is not a simple protocol, and you've still got a lot to learn about how the simple stuff works. You are curently relying on the socket state and the format of the data to determine the data flow - but HTTP uses neither.

If you want to learn how HTTP works, then there's lots of documentation on the internet - the RFC's are a must, but are not a gentle introduction. If your objective is to develop an application, then use curl (or even the HTTP stream wrappers).

share|improve this answer
 
Hi, I did send the '\0' from the server but still it does not return empty string. Alternatively I just send ## from the server then check ## in php to end the loop. I am aware of the curl, not trying to write a HTTP client. I just want the php to communicate to the c++ winsock application that I have written. Thanks for the help. –  William Mar 14 '13 at 3:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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