Hey all, new here..

I seem to be having problems with how fread() and fgets() works on socket streams.

I've found that if I read past the end of the socket buffer using either fgets() or fread() the script will wait for the timeout of that socket set by stream_set_timeout() before returning the buffer.

I've tried setting stream_set_blocking() to false but that gives me undesired results because fgets() and fread() won't wait for anything to arive in the buffer (if it's blank) before returning it, thus returning false when I want it to wait for something.

If I set the socket timeout to 2 seconds it will wait 2 seconds before returning the buffer if the length specified in fgets($sock, 1024) and fread($sock, 1024) is longer than the length of the buffer.

Needless to say this is undesirable as it really impacts the preformance and speed of the script.

What I need is a way to know what the actuall length of the buffer (unread chars). If I can accomplish this I will be able to know if it should wait for more chars to appear in the buffer or just return what's already in it. Make sense?

I've tried using fgetc() while looping, but that doesn't work for what I need either.

Anyone have any answers to this problem?

Thanks in advance. =).
- Jeremy

    No, I'm not getting a timeout. And I have plenty of error handling in the script to make sure the connection was complete and such. The script works, it's just slow.

    What I'm asking is for a way to get the number of unread chars that still reside in the buffer. fgets() and fread() will timeout if they read past the end of the buffer, they wait for more data to come into the buffer before returning what was already in it.

    I want it to work something like this:

    if there's data in the buffer return all of it without waiting for more, else wait for some to arrive.

    The way it works currently.

    if there's data in the buffer return it, but before doing so wait for more, else wait for some to arrive.

    Thanks for the reply. =).

      what's up with this reply?

      do the forum mods here think that's ok?

        2 years later

        jejacks0n, i would like to discuss this with you if youre still online.

          If you have control of what's on the other side of the socket, then you can have it dump the total size at the beginning. However, if you don't, then the last byte in the buffer should be either \n or \r or null, which is hex 00.

          You can use the function

          socket_read($sock, 1024, PHP_BINARY_READ);
          
          // or try this
          
          socket_read($sock, 1024, PHP_NORMAL_READ);

          or you can loop and read byte by byte until you match \n or \r or null.

          If none of the above works, read the buffer byte by byte and print the hex value of each byte. Once the display stops, look at the last byte and hopefully that's the value used to indicate the end.

          Hope this helps! Good luck.

            Write a Reply...