views:

149

answers:

1

My fwrite to a socket is not flushed, until the socket closes. How to change? I want it to flush after each fwrite.

I've tried:

1) flush()
2) fflush()
3) ob_implicit_flush(true);

None of those worked, I still had to quit php for my socket to receive the data.


Including some sample code, anything looks wrong?

while($clientSocket = socket_accept($this->serviceConnection))
    {                              
        while( $clientMessage = socket_read($clientSocket, 1024)     )                     
        {                  
            echo 'Relaying message to server: ' . $clientMessage;
            if( !fwrite($this->Connection, $clientMessage) )
                echo 'Error writing to server'; 
            fflush($this->Connection);
        }
        socket_close($clientSocket);
    }
+2  A: 

There is a function for that! fflush($rc)

Did you make sure you had the correct resource as the parameter?

edit

Okay. looking at your code you are using socket_* functions. You need to use fsockopen, fwrite, fflush, fsockclose. Your code should work the same, but you will be able to flush output. check out the example in the php manual for more details.

Byron Whitlock
yea, does it work with network resource created by fsockopen? The doc says only file handles.
erotsppa
The php manpage for fflush says,"The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose())."
Steve-o