Re: strange TCP stack behiviour with write()es in pieces

From: Richard B. Johnson (
Date: Wed Jan 02 2002 - 15:21:46 EST

On Wed, 2 Jan 2002, dean gaudet wrote:

> On Wed, 2 Jan 2002, Michal Moskal wrote:
> > void send_packet(int cmd, void *data, int len)
> > {
> > struct header h = { cmd, len };
> >
> > write(fd, &h, sizeof(h));
> > write(fd, data, len);
> > }
> you should look into writev(2).

First, this isn't "TCP stack behavior...". It's an apparent attempt
to write raw (network?) packets using some kernel primitives. I presume
that you have obtained the fd from either socket() or by opening some
device. Whatever. If you are generating a "packet", you need to
make the packet in a buffer and send the packet. You can't presume
that something will concatenate to separate writes into some
kind of "packet". If the hardware is Ethernet, even the hardware
will fight you because it puts a destination-hardware-address,
source-hardware-address, packet-length, data (your packet), then
32-bit CRC into the outgoing packet. FYI, that 'data' is where
the TCP/IP data-gram exists.

That said, if you are trying to make some kind of "zero-copy" thing,
you need to leave space in the initial allocation for the header and
other overhead. That way, you do one write to the device.

Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (797.90 BogoMips).

    I was going to compile a list of innovations that could be
    attributed to Microsoft. Once I realized that Ctrl-Alt-Del
    was handled in the BIOS, I found that there aren't any.

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

This archive was generated by hypermail 2b29 : Mon Jan 07 2002 - 21:00:18 EST