LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2021, 07:50 AM   #1
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 684

Rep: Reputation: 34
[php] fread() only reads 8192 bytes


Hello

I have the following code, receiving a continuous tcp stream of data (to analyse).
Code:
$socket = fsockopen("127.0.0.1","5038", $errno, $errstr, 5);
$wrets = fread($socket, 9216);
echo "mb_strlen:".mb_strlen($wrets, '8bit')."\n";
I notice that mb_strlen is at max 8192, which causes certain data to be "cut off".

I wonder how (and where) I can increase this value of 8192 ?
 
Old 04-26-2021, 08:01 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,339

Rep: Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133

I put "php fread 8192" into a search engine and it returns the PHP manual page for fread.

The manual page says why it happens, and has two distinct examples that will solve your problem.


Last edited by boughtonp; 04-26-2021 at 08:05 AM.
 
1 members found this post helpful.
Old 04-27-2021, 08:16 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,140
Blog Entries: 4

Rep: Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227
To clarify ...

If you need to "read a potentially-unlimited amount of data," you simply can never expect to "read it all at once." Instead, you must read it "a 'chunk' at a time," in a loop ... repeating this process until you finally run out of "chunks." (Having no knowledge, and no control, over the size of a "chunk.")

Each call to fread() will tell you how many bytes it managed to read, from "the current position," "this time." Plan accordingly. It is entirely unpredictable how many times you will need to repeat this algorithm.

Last edited by sundialsvcs; 04-27-2021 at 08:17 PM.
 
2 members found this post helpful.
Old 04-28-2021, 12:34 PM   #4
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 684

Original Poster
Rep: Reputation: 34
I "solved" it by doing the following :
Code:
$streamLenght=mb_strlen($wrets, '8bit');
//echo "mb_strlen:".$streamLenght."\n";
if ($streamLenght % 8192 == 0) {
    // there is more data to come
} else {
   // perform action on streamData
}
 
Old 04-28-2021, 12:55 PM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,339

Rep: Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133

*shrug*

You could actually solve it if you started with either of the examples in the documentation.

 
Old 04-28-2021, 01:04 PM   #6
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 684

Original Poster
Rep: Reputation: 34
Maybe other readers like your very vague answers, but what I wanted to do is give the exact part of my code which helped me capture the stream of data with random length from an external program, and execute my logic further ONLY when all data is actually received.
 
Old 04-28-2021, 07:54 PM   #7
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 488
Blog Entries: 4

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by sundialsvcs View Post
To clarify ...
Welcome back, sundialsvcs!
 
Old 04-29-2021, 09:38 AM   #8
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,373

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Hi

There is a bit about using fread on streams other than files in the PHP manual.

Quote:
Warning

When reading from anything that is not a regular local file, such as streams returned when reading remote files or from popen() and fsockopen(), reading will stop after a packet is available. This means that you should collect the data together in chunks as shown in the examples below.
https://www.php.net/manual/en/function.fread.php

And below it is 2 ways to solve the problem. Using stream_get_contents instead of fread, looks like the easiest solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
samba socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192 anon091 Linux - Server 2 03-20-2013 09:06 AM
[SOLVED] reading 4 bytes at a time fread/fseek question. PoleStar Programming 2 04-23-2012 01:14 AM
Difference between buffered disk reads and cached reads? pinga123 Linux - Newbie 1 07-13-2011 11:40 AM
HttpClient and 8192 bytes Sunday03 Programming 2 07-11-2010 09:24 PM
Samba - When is "socket options = SO_SNDBUF=8192 SO_RCVBUF=8192" Nessesary? JeffC1 Linux - Software 4 09-05-2008 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration