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

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

Hi List

I am working with an application that has to run as a socket listener. It
receives a string from another application on a certain socket and then does
some processing and then p***es the result back to the client application via
the socket. Here is the listener code I am using

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

$arev_data = socket_read($client, 1024);

$yresponse = build_XML_request($arev_data);

socket_write($client, $yresponse);

socket_close($client);

}
socket_close($socket);

The application is working fine but I have just noticed that when the $yresponse
variable is larger than 1024 bytes the string returned to the client is
truncated. I just had a look at the php manual for socket_write and it
mentions that truncating the data at the buffer size can be a problem. The
manual does not offer any possible solutions though. I am stumped here as the
value for $yresponse will often be larger than 1024 bytes and its length is
quite variable.

Does anyone have any suggestions on how I can make sure the entire contents of
$yresponse are p***ed back to the client?




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] socket_write buffer problem
Old
  (#2)
Jochem Maas
Guest
 
Posts: n/a
Default Re: [PHP] socket_write buffer problem - 05-14-2007, 03:36 AM

Richard Luckhurst wrote:
> Hi List
>
> I am working with an application that has to run as a socket listener. It
> receives a string from another application on a certain socket and then does
> some processing and then p***es the result back to the client application via
> the socket. Here is the listener code I am using
>
> while(true)
> {
> $client = socket_accept($socket);
>
> $arev_data = socket_read($client, 1024);
>
> $yresponse = build_XML_request($arev_data);
>
> socket_write($client, $yresponse);
>
> socket_close($client);
>
> }
> socket_close($socket);
>
> The application is working fine but I have just noticed that when the $yresponse
> variable is larger than 1024 bytes the string returned to the client is
> truncated. I just had a look at the php manual for socket_write and it
> mentions that truncating the data at the buffer size can be a problem. The
> manual does not offer any possible solutions though. I am stumped here as the
> value for $yresponse will often be larger than 1024 bytes and its length is
> quite variable.
>
> Does anyone have any suggestions on how I can make sure the entire contents of
> $yresponse are p***ed back to the client?


I would start by reading the relevant page:

http://php.net/socket_write

after reading that your first attempt will probably be this
(you may have complication is your using a multibyte charset):

socket_write($client, $yresponse, strlen($yresponse));

if that doesn't work, you will be looking at capturing the return value
of socket_write() in order to determine how many bytes were sent ...
this information will allow to to use a loop to call socket_write as many times
as is required to send all the data. hope that makes sense to you.

>
>
>
>
> 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[2]: [PHP] socket_write buffer problem
Old
  (#3)
Richard Luckhurst
Guest
 
Posts: n/a
Default Re[2]: [PHP] socket_write buffer problem - 05-14-2007, 03:36 AM

Hi Jochem,


JM> I would start by reading the relevant page:

JM> http://php.net/socket_write

JM> after reading that your first attempt will probably be this
JM> (you may have complication is your using a multibyte charset):

As I had said in my earlier post I had already read the php manual section for
socket_write.

JM> socket_write($client, $yresponse, strlen($yresponse));

JM> if that doesn't work, you will be looking at capturing the return value
JM> of socket_write() in order to determine how many bytes were sent ...
JM> this information will allow to to use a loop to call socket_write as many times
JM> as is required to send all the data. hope that makes sense to you.

I tried the strlen($yresponse) length option in socket_write and it did not
matter if I set socket_write to anything larger than 1024. It appears that there
is a buffer with a size of 1024 and in my case the length option would only be
of use if the string I wanted to send was shorter than the size of the buffer.

Rather than capturing the return value of socket_write I broke my test string
into 2 chunks as the total was less than 2048 bytes.

What I tried was

// grab first 1024 bytes
$buffer = substr($yresponse,0,1024);
// send first 1024 bytes
socket_write($client, $buffer);
// replace first 1024 bytes with ''
$buffer = substr_replace($yresponse,'',0,1024);
//send the remainder of the string
socket_write ($client, $buffer);

What I found was that the first 1024 bytes was sent but I still never saw the
second 1024 bytes. It seem the second socket_write is not happening.

Am I on the right track here?

>>
>>
>>
>>
>> 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
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