RE: Sockets (reading)

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

 



From: Shawn McKenzie
> Bob McConnell wrote:
>> From: Philip Thompson
>>> During a socket read, why would all the requested number of bytes
not
>> 
>>> get sent? For example, I request 1000 bytes:
>>>
>>> <?php
>>> $data = @socket_read ($socket, 2048, PHP_BINARY_READ);
>>> ?>
>>>
>>> This is actually in a loop, so I can get all the data if split up.
So,
>> 
>>> for example, here's how the data split up in 3 iterations (for 1000

>>> bytes):
>>>
>>> 650 bytes
>>> 200 bytes
>>> 150 bytes
>>>
>>> But if I can accept up to 2048 bytes per socket read, why would it
not
>> 
>>> pull all 1000 bytes initially in 1 step? Any thoughts on this would
be
>> 
>>> greatly appreciated!
>> 
>> Because that's the way TCP/IP works, by design. TCP is a stream
>> protocol. It guarantees all of the bytes written to one end of the
pipe
>> will come out the other end in the same order, but not necessarily in
>> the same groupings. There are a number of buffers along the way that
>> might split them up, as well as limits on packet sizes in the various
>> networks it passed through. So you get what is available in the last
>> buffer when a timer expires, no more, and no less.
>> 
>> If you have serialized data that needs to be grouped in specific
blocks,
>> your application will need to keep track of those blocks,
reassembling
>> or splitting the streamed data as necessary. You could use UDP which
>> does guarantee that packets will be kept together, but that protocol
>> doesn't guarantee delivery.
> 
> I'm not sure this has much to do with the OP's problem, but this part
is
> backwards.  TCP is connection oriented and tracks segments by sequence
> number for each connection.  This enables the stack to pass the data
in
> order to the higher layers.  UDP is connectionless and has no way to
> determine what datagram was sent before the other one, so it is up to
> the higher layers to reassemble.  As for IP in general, if packets
need
> to be fragmented along the way by a router in order to fit the MTU of
a
> different network, then the IP stack on the receiving end will
> reassemble the fragments based upon information that the router
injects
> into the fragments.

Shawn,

You're looking at it inside out. Yes, the individual packets are tracked
by the stack, to make sure they arrive in the correct order. But the
size and fragmentation of those packets have no relationship at all to
any data structure the application layer may imply. They simply
implement a communications stream to reliably move octets from one point
to another. If the application needs structure, it has to manage that
for itself.

For UDP, if you write a 32 byte packet, the matching read will get a 32
byte packet, if it arrived at the receiving stack. Missed data detection
and retry requests are left up to the application.

Bob McConnell

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