note 81269 added to function.fwrite
| From: | dharris dot nospam at removethispart dot drh dot net | Date: | Wed, 20 Feb 2008 18:47:02 +0000 |
| Subject: | note 81269 added to function.fwrite | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136321@lists.php.net to get a copy of this message | ||
Some people say that when writing to a socket not all of the bytes requested to be written may be
written. You may have to call fwrite again to write bytes that were not written the first time. (At
least this is how the write() system call in UNIX works.)
This is helpful code (warning: not tested with multi-byte character sets)
function fwrite_with_retry($sock, &$data)
{
$bytes_to_write = strlen($data);
$bytes_written = 0;
while ( $bytes_written < $bytes_to_write )
{
if ( $bytes_written == 0 ) {
$rv = fwrite($sock, $data);
} else {
$rv = fwrite($sock, substr($data, $bytes_written));
}
if ( $rv === false || $rv == 0 )
return( $bytes_written == 0 ? false : $bytes_written );
$bytes_written += $rv;
}
return $bytes_written;
}
Call this like so:
$rv = fwrite_with_retry($sock, $request_string);
if ( ! $rv )
die("unable to write request_string to socket");
if ( $rv != strlen($request_string) )
die("sort write to socket on writing request_string");
----
Server IP: 209.41.74.194
Probable Submitter: 12.226.111.244
----
Manual Page -- http://www.php.net/manual/en/function.fwrite.php
Edit -- https://master.php.net/note/edit/81269
Del: integrated -- https://master.php.net/note/delete/81269/integrated
Del: useless -- https://master.php.net/note/delete/81269/useless
Del: bad code -- https://master.php.net/note/delete/81269/bad+code
Del: spam -- https://master.php.net/note/delete/81269/spam
Del: non-english -- https://master.php.net/note/delete/81269/non-english
Del: in docs -- https://master.php.net/note/delete/81269/in+docs
Del: other reasons-- https://master.php.net/note/delete/81269
Reject -- https://master.php.net/note/reject/81269
Search -- https://master.php.net/manage/user-notes.php