Re: Re: socket_write buffer problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Luckhurst wrote:
 I have been banging my head against a wall all day trying to work
 this problem out. To recap my earlier post, I have a simple socket
 listener that handles the incoming string fine with socket_read and
 then it fires off the application as it should. When it returns the
 data to the socket only the first 1024 bytes get sent.

 I have done some testing and for the life of me I can not find a way
 to loop through the buffer. Everything I try still results in only
 the first 1024 bytes.
<snipped a whole load of crap that nobody cares about>
 Does anyone have any clues?

You've already been given a clue... read the manual: http://php.net/socket_write

I know you said you read it, but you obviously haven't. To quote...

"Returns the number of bytes successfully written to the socket or *FALSE* on error"

You are not even checking the return value from socket_write for an error, nevermind to see the number of bytes actually sent. The socket_write function, like many other similar functions, is not guaranteed to send exactly what you give it, even if you provide the third parameter.

Your code for sending data should be structured something like the following *untested* code...

$len = strlen($datatosend);
$offset = strlen($datatosend);
while ($offset < $len)
{
$sent = socket_write($client, substr($datatosend, $offset), $len - $offset);
   if ($sent === false)
   {
      // An error occurred, break out of the while loop
      break;
   }
   $offset += $sent;
}

if ($offset < $len)
{
// An error occurred, use *socket_last_error()* <http://uk2.php.net/manual/en/function.socket-last-error.php>* and **socket_strerror()* <http://uk2.php.net/manual/en/function.socket-strerror.php> to find out what happened
}
else
{
   // Data sent ok
}

Your problem is that you made lazy assumptions, and we all know that assumptions are the mother of all fsckups. Now please irradiate your hands and try again.

-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux