note 95127 deleted from function.stream-get-line by danbrown
| From: | danbrown@php.net | Date: | Tue, 15 Dec 2009 03:29:14 +0000 |
| Subject: | note 95127 deleted from function.stream-get-line by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163984@lists.php.net to get a copy of this message | ||
Note Submitter: pk at ritm dot ru
----
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