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
 
Edited by jmurch
Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.