note 88921 modified in function.socket-write by danbrown

From: Date: Fri, 13 Feb 2009 14:02:34 +0000
Subject: note 88921 modified in function.socket-write by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150621@lists.php.net to get a copy of this message
"socket_write() does not necessarily write all bytes from the given buffer." So I wrote the following code to correctly write message to the socket <?php $message="Message to sent"; $len = strlen($message); $offset = 0; while ($offset < $len) { $sent = socket_write($socket, substr($message, $offset), $len-$offset); if ($sent === false) { // Error occurred, break the while loop break; } $offset += $sent; } if ($offset < $len) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); echo "SENDING ERROR: $errormsg"; } else { // Data sent ok } ?> --was-- "socket_write() does not necessarily write all bytes from the given buffer." So I wrote the following code to correctly write message to the socket $message="Message to sent"; $len = strlen($message); $offset = 0; while ($offset < $len) { $sent = socket_write($socket, substr($message, $offset), $len-$offset); if ($sent === false) { // Error occurred, break the while loop break; } $offset += $sent; } if ($offset < $len) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); echo "SENDING ERROR: $errormsg"; } else { // Data sent ok } http://php.net/manual/en/function.socket-write.php

« previous php.notes (#150621) next »