Jump to content

Reading socket stream


jmurch

Recommended Posts

I am reading a stream using '$input = trim(fread($socket, 16384));' and everything is working fine until the length of data from the stream exceeds 4078.  At  4078 I stop for some reason reading the stream.  

 

I am wondering why this works (up to 4078):  '$input = trim(fread($socket, 16384));'        

But this does not: '$input = stream_get_contents($socket);'

 
 
Other parameters for the stream are:
 
        stream_set_blocking($con, true);
        stream_socket_enable_crypto($con, true, STREAM_CRYPTO_METHOD_TLS_SERVER);
        handle_incoming_connection($con, $client);
 
 
$res = stream_context_create($streamopts);
if ($res === false) {
    System_Daemon::err('error when creating stream');
    System_Daemon::stop();
    exit(1);
}
 
$master = stream_socket_server(
        "tcp://$host:$port", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $res
);
if ($master === false) {
    System_Daemon::err('stream_socket_server(): [%s] %s', $errno, $errstr
    );
    System_Daemon::stop();
    exit(1);
}
stream_socket_enable_crypto($master, false);
 
 
function handle_incoming_connection($socket, $client) {
 
       $input = trim(fread($socket, 16384));
        if (($input == null)
                || ($input == 'exit')
        ) {
            return;
        }
 
 
 
Any help would be greatly appreciated.
 
TIA, Jeff
 
Link to comment
https://forums.phpfreaks.com/topic/275370-reading-socket-stream/
Share on other sites

... At 4078 I stop for some reason reading the stream. ...

What exactly does this mean? Does the script abort with no output, or does the script hang up?

 

With a BLOCKING stream, fread is going to wait ("block") until it gets (in this case) 16,384 bytes of data. So the script may seem to hang up. If the script is aborting, you may be exhausting the memory limits of PHP.

 

Make sure you have error reporting turned on so you can see the memory error.

 

Note: please use [ code] [ /code] tags around your code when posting on the forum. It makes it much easier to read.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.