note 95127 added to function.stream-get-line
| From: | pk at ritm dot ru | Date: | Mon, 14 Dec 2009 22:38:31 +0000 |
| Subject: | note 95127 added to function.stream-get-line | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163963@lists.php.net to get a copy of this message | ||
some info that can be useful (extension of previous message)
if you want to use function fgets in your php tcp server, you should know that fgets is not adapted
for packet tcp servers, ans stream_get_line is!
difference here:
For example, client want to send packet to server with total length of 100 bytes + ending delimiter
\n (total=101b)
Lets imagine that client will send 50 bytes of this packet because of its slow internet connection,
wait 1-2 seconds, and then send data again.
what will do server with function fgets?
<?php
// fgets server
stream_set_timeout($s, 0, 0);
stream_set_blocking($s, 0);
sleep(10); // in this interval client will send 1st part of data
var_dump( fgets($s, 2048) );
sleep(10); // now client will send 2nd part of data
var_dump( fgets($s, 2048) );
// output will be:
// string(50) "data here .."
// string(51) "data here ... \n"
?>
what will do server with function stream_get_line?
<?php
// stream_get_line server
stream_set_timeout($s, 0, 0);
stream_set_blocking($s, 0);
sleep(10); // in this interval client will send 1st part of data
var_dump( stream_get_line($s, 2048, "\n") );
sleep(10); // now client will send 2nd part of data
var_dump( stream_get_line($s, 2048, "\n") );
// output will be:
// bool(false)
// string(100) "data here .."
?>
what we have total?
function stream_get_line is very smart and wait for full packet instead of fgets
----
Server IP: 78.41.111.5
Probable Submitter: 92.100.77.253
----
Manual Page -- http://www.php.net/manual/en/function.stream-get-line.php
Edit -- https://master.php.net/note/edit/95127
Del: integrated -- https://master.php.net/note/delete/95127/integrated
Del: useless -- https://master.php.net/note/delete/95127/useless
Del: bad code -- https://master.php.net/note/delete/95127/bad+code
Del: spam -- https://master.php.net/note/delete/95127/spam
Del: non-english -- https://master.php.net/note/delete/95127/non-english
Del: in docs -- https://master.php.net/note/delete/95127/in+docs
Del: other reasons-- https://master.php.net/note/delete/95127
Reject -- https://master.php.net/note/reject/95127
Search -- https://master.php.net/manage/user-notes.php