Board index » delphi » Sockets and minimum packet size

Sockets and minimum packet size

Is there a minimum packet size that will be received by a TServerSocket /
TClientSocket component in Delphi 5/6? I have found that the maximum size is
8k, but "Carl" indicated on 04/11/02 (Re: Socket trouble) "that small data
messages or files will sometimes be broken up into even smaller packets."

So far all the WinSock literature I have seen suggests that there is no
lower limit to the number of bytes that will be received. This presents a
problem when parsing tags in the received transmission (i.e. long
descriptive XML / HTML style tags). It would seem that the best approach
might be to buffer off the data and parse it for completeness. If it doesn't
pass muster according to the protocol then just append any further data
received from the IP / port to the buffer and parse again. The trick is
deciding when to toss received data if the sender is not obeying the
protocol or is a hacker just tickling the port to see what shakes loose.

It's really a shame if I can only be guaranteed that the minimum packet size
is but one byte, as the scenario above suggests...

Zachary Hartnett
Micro Software Services, Inc.
www.Editrade.com

 

Re:Sockets and minimum packet size


Yup, the minimum packet size is 1 byte. You can never rely on the size of
the packets that you receive, who knows how many routers, etc the data goes
through before it gets to you and how those devices change the size of the
packets that left the server. Most clients (HTML/XML/text type clients
anyway) receive the entire message (based on the Content-Length header)
before parsing it. What you can do is look at the content-length header and
decide if there is too much data to process then just disconnect the client.
Your method would of course work but has possibly more overhead as you may
be processing data that you'll be dumping anyway.

Sometimes the server does not send a content-length header but rather
"chunked-data". If you're in control of the server then this would be the
way to go. If you're writing a server that produces dynamic output where the
complete content-length is unknown, then what you do is send it in chunks.
The way chunked data works is as follows: The http header is sent (without a
content-length header, but indicating "chunked-data") Then a hex value is
sent indicating the length of the following chunk, then the chunk of data
itself, then another hex value, and another chunk, and so on.. This would
allow your client to receive a chunk at a time (received in x amount of
packets) and process it, then receive the next chunk and process that, and
so on until a 0 chunk is received. I wrote a server a couple of years ago
that did this. The data looks something like

HTTP/1.1 200 OK
content-type: chunked (Can't remember the header exactly)
date: 2002-05-10

17
<XML>instruction1</XML>
0F
<XML>ins2</XML>
0

This is a standard http function, look it up in the http protocol
documentation and see if your server can do this for you.

Paul

"Zachary Hartnett >" <<None> wrote in message news:3cdb0af7$1_1@dnews...

Quote
> Is there a minimum packet size that will be received by a TServerSocket /
> TClientSocket component in Delphi 5/6? I have found that the maximum size
is
> 8k, but "Carl" indicated on 04/11/02 (Re: Socket trouble) "that small data
> messages or files will sometimes be broken up into even smaller packets."

> So far all the WinSock literature I have seen suggests that there is no
> lower limit to the number of bytes that will be received. This presents a
> problem when parsing tags in the received transmission (i.e. long
> descriptive XML / HTML style tags). It would seem that the best approach
> might be to buffer off the data and parse it for completeness. If it
doesn't
> pass muster according to the protocol then just append any further data
> received from the IP / port to the buffer and parse again. The trick is
> deciding when to toss received data if the sender is not obeying the
> protocol or is a hacker just tickling the port to see what shakes loose.

> It's really a shame if I can only be guaranteed that the minimum packet
size
> is but one byte, as the scenario above suggests...

> Zachary Hartnett
> Micro Software Services, Inc.
> www.Editrade.com

Re:Sockets and minimum packet size


Perfect. This gets me off on the right track. Many thanks!

"Paul Gertzen" <paulg@net-1 dot org> wrote in message
news:3cdb6d4e_1@dnews...

Quote
> Yup, the minimum packet size is 1 byte. You can never rely on the size of
> the packets that you receive, who knows how many routers, etc the data
goes
> through before it gets to you and how those devices change the size of the
> packets that left the server. Most clients (HTML/XML/text type clients
> anyway) receive the entire message (based on the Content-Length header)
> before parsing it. What you can do is look at the content-length header
and
> decide if there is too much data to process then just disconnect the
client.
> Your method would of course work but has possibly more overhead as you may
> be processing data that you'll be dumping anyway.

> Sometimes the server does not send a content-length header but rather
> "chunked-data". If you're in control of the server then this would be the
> way to go. If you're writing a server that produces dynamic output where
the
> complete content-length is unknown, then what you do is send it in chunks.
> The way chunked data works is as follows: The http header is sent (without
a
> content-length header, but indicating "chunked-data") Then a hex value is
> sent indicating the length of the following chunk, then the chunk of data
> itself, then another hex value, and another chunk, and so on.. This would
> allow your client to receive a chunk at a time (received in x amount of
> packets) and process it, then receive the next chunk and process that, and
> so on until a 0 chunk is received. I wrote a server a couple of years ago
> that did this. The data looks something like

> HTTP/1.1 200 OK
> content-type: chunked (Can't remember the header exactly)
> date: 2002-05-10

> 17
> <XML>instruction1</XML>
> 0F
> <XML>ins2</XML>
> 0

> This is a standard http function, look it up in the http protocol
> documentation and see if your server can do this for you.

> Paul

> "Zachary Hartnett >" <<None> wrote in message news:3cdb0af7$1_1@dnews...
> > Is there a minimum packet size that will be received by a TServerSocket
/
> > TClientSocket component in Delphi 5/6? I have found that the maximum
size
> is
> > 8k, but "Carl" indicated on 04/11/02 (Re: Socket trouble) "that small
data
> > messages or files will sometimes be broken up into even smaller
packets."

> > So far all the WinSock literature I have seen suggests that there is no
> > lower limit to the number of bytes that will be received. This presents
a
> > problem when parsing tags in the received transmission (i.e. long
> > descriptive XML / HTML style tags). It would seem that the best approach
> > might be to buffer off the data and parse it for completeness. If it
> doesn't
> > pass muster according to the protocol then just append any further data
> > received from the IP / port to the buffer and parse again. The trick is
> > deciding when to toss received data if the sender is not obeying the
> > protocol or is a hacker just tickling the port to see what shakes loose.

> > It's really a shame if I can only be guaranteed that the minimum packet
> size
> > is but one byte, as the scenario above suggests...

> > Zachary Hartnett
> > Micro Software Services, Inc.
> > www.Editrade.com

Other Threads