Subject: [Nel] newbies about packet schema

From: "jaleco" <[email-masked]>
Date: Tue, 6 Mar 2001 12:05:42 +0800

I have read artice for winsock program, 
if folling article is true, that snow ball netowrk driver , should change it's packet schema.
any one can give me suggect ?

original from http://www.cyberport.com/~tangent/programming/winsock/intermediate.html#disable-nagle
Inexperienced Winsockers usually try disabling the Nagle algorithm when they are trying to impose some kind of packet scheme on a TCP data stream. That is, they want to be able to send, say, two packets, one 40 bytes and the other 60, and have the receiver get a 40-byte packet followed by a separate 60-byte packet. (With the Nagle algorithm enabled, TCP will often coalesce these two packets into a single 100 byte packet.) Unfortunately, this is futile, for the following reasons:

  1.. Even if the sender manages to send its packets individually, the receiving TCP/IP stack may still coalesce the received packets into a single packet. This can happen any time the sender can send data faster than the receiver can deal with it. 
  2.. Winsock Layered Service Providers (LSPs) may coalesce or fragment stream data, especially LSPs that modify the data as it passes. 
  3.. Turning off the Nagle algorithm in a client program will not affect the way that the server sends packets, and vice versa. 
  4.. Routers and other intermediaries on the network can fragment packets, and there is no guarantee of "proper" reassembly with stream protocols. 
  5.. If packet arrives that is larger than the available space in the stack's buffers, it may fragment a packet, queuing up as many bytes as it has buffer space for and discarding the rest. (The remote peer will resend the remaining data later.) 
  6.. Winsock is not required to give you all the data it has queued on a socket even if your recv() call gave Winsock enough buffer space. It may require several calls to get all the data queued on a socket.

---

From: Kenneth Duda <[email-masked]>
Date: Sat, 3 Mar 2001 02:02:34 -0800

> I have read artice for winsock program, if folling article is
> true, that snow ball netowrk driver , should change it's packet
> schema.  any one can give me suggect ?

I am not familiar with the snowball protocol ("packet schema").
However, I assure you that the article is correct --- attempting
to turn TCP from a byte stream into a sequenced message stream by
disabling Nagle will not work.  The only way to implement message
semantics on top of TCP is to add your own packetization to the
byte stream.  For example,

  void SendMessage( int socket, const void * buf, size_t length ) 
  {
    long l = htonl( length );
    write( socket, &l, 4 );
    write( socket, buf, length );
  }

  void RecvMessage( int socket, void * buf, size_t * length ) 
  {
    long l;
    read( socket, &l, 4 );
    l = ntohl( l );
    assert( l < *length );
    *length = l;
    read( socket, buf, length );
  }

-Ken

Kenneth J. Duda
Stanford University Distributed Systems Group
<[email-masked]>