Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
Re: socket_write buffer problem
Old
  (#1)
Richard Luckhurst
Guest
 
Posts: n/a
Default Re: socket_write buffer problem - 05-14-2007, 03:36 AM

Hi List

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.

Here is the code I have been trying

while(true)
{
$client = socket_accept($socket);

$arev_data = socket_read($client, 1024);

$yresponse = build_XML_request($arev_data);

if (strlen ($yresponse) < 1024)
{
socket_write($client, $yresponse);
}
else
{
while (strlen($yresponse) > 0)
{
if (strlen ($yresponse) > 1024)
{
$out = substr($yresponse, 0, 1024);
socket_write($client, $out, strlen($out));
$yresponse = substr_replace($yresponse,'',0,1024);
}
else
{
socket_write($client, $yresponse);
print "$yresponse ";
$yresponse = substr_replace($yresponse,'',0,strlen($yresponse)) ;
}

}

}

socket_close($client);
}

Just for testing I cave an if statement that takes care of the case where there
is a data string of less than 1024 bytes. The problem starts when I get into the
else case where the$response is larger than 1024 bytes. The if statement seems
OK and the first time through when the test data is 1521 bytes the first 1024
bytes gets returned by the socket_write. I then get rid of the first 1024 bytes
of the string and the if statement falls through to the else as the string is
now only 497 bytes left in the $ypsilon string. I see nothing returned to the
client by the socket_write here however the print "$yresponse \n"; prints the
data. I can not work out why the socket_write is not writing the data to the
socket at this point.

Does anyone have any clues?

Regards,
Richard Luckhurst
Product Development
Exodus Systems - Sydney, Australia.
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: [PHP] Re: socket_write buffer problem
Old
  (#2)
Stut
Guest
 
Posts: n/a
Default Re: [PHP] Re: socket_write buffer problem - 05-14-2007, 03:36 AM

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 ***umptions, and we all know that
***umptions are the mother of all fsckups. Now please irradiate your
hands and try again.

-Stut
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] Re: socket_write buffer problem
Old
  (#3)
Roman Neuhauser
Guest
 
Posts: n/a
Default Re: [PHP] Re: socket_write buffer problem - 05-14-2007, 03:36 AM

# EMAIL REMOVED / 2007-01-22 11:33:51 +0000:
> EMAIL REMOVED wrote:
> >> 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...

> >
> > Now you are making an ***umption and you are making a "fsckup". Just
> > because I did not show in my simplified example that I had tested the
> > number of bytses sent does not mean for a second that I had not done
> > it.


Richard,

I had almost sent a message very similar to the one from Stut you're
complaining about here, but cancelled it because I expected you would
react the way you have, and I'd just waste energy.

One last try so that you know Stut is not alone in his ***essment:

You might have actually played with a "simplified example", but judging
from your previous questions (and reactions to replies), you don't pay
much attention to your own observations, and/or you make too many
***umptions, and your subsequent ****ups bring you here.

You come claiming it's broken, you've tried everything, and show us code
that's simply broken. We never see the correct code you claim to have
tried (after you've been told to fix bugs in what you have shown).

Programming is not like waving a magic wand, it's not our fault the
computer does what you tell it instead of what you want it to do!

Until-you-pull-your-head-from-wherever-it-is'ly yours,
....

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] Re: socket_write buffer problem
Old
  (#4)
Jochem Maas
Guest
 
Posts: n/a
Default Re: [PHP] Re: socket_write buffer problem - 05-14-2007, 03:36 AM

hi Richard,

Richard Luckhurst wrote:
> Hi List
>


.....

> Does anyone have any clues?


sorry to ***ume you hadden not read the manual, but it does seem your
not taking onboard the bit about the return value of socket_write().

please revise you code in the manner Stut suggested (checking/using the return
value of scket_write()) and come back if that doesn't work.

>
> Regards,
> Richard Luckhurst
> Product Development
> Exodus Systems - Sydney, Australia.
>

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] Re: socket_write buffer problem
Old
  (#5)
Stut
Guest
 
Posts: n/a
Default Re: [PHP] Re: socket_write buffer problem - 05-14-2007, 03:36 AM

Please keep it on-list, even if you feel you need to have a go at me -
they love it!

EMAIL REMOVED wrote:
> > 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...

>
> Now you are making an ***umption and you are making a "fsckup". Just
> because I did not show in my simplified example that I had tested the
> number of bytses sent does not mean for a second that I had not done
> it.


I'm going by the information you have supplied. You cannot possibly
expect us to help you if you do not provide the correct information.

> The number of bytes sent was 1024 for the first iteration of the loop
> and none for the second. The socket_write function returned a status
> of "SUCCESS".


Please show us the *actual* code you are using. Without that we don't
have any real chance of finding the problem.

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

>
> I believe you are making an "lazy ***umption" here as you have no
> idea what I have tried. While I appreciate help I believe your
> response was rude and not very helpful to a newbie to PHP.


I apologise if you took my response to be rude, that was not my intention.

As I said before, if you want help you need to give us accurate
information. We're not mind readers.

-Stut

PS. Look into my eyes, just the eyes, not around the eyes, into my eyes.
You're under. When you wake up you will realise that you were not
offended by my post, you were just embarr***ed because you failed the
full disclosure test. 3..2..1.. and you're back.
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top