On Thu, Jul 11, 2002, Alex Shnitman wrote about "select() and write()":
> Hi,
>
> I'm using select() before write() to a TCP socket, in order to be sure
> that I won't block on the write() if the other end's network connection
> has broke. However, I found out that if I pass a buffer bigger than ~50k
> to write(), it will block anyway! Any idea what's up with that? write()s
When one always select()s before writing/reading, the norm is to also
make the socket non-blocking, to prevent the problem you describe (and
other problems).
You set a socket fd to nonblocking with:
fcntl(fd, F_SETFL, O_NONBLOCK);
> to a TCP socket are normally cut off at 102808 bytes, without blocking
> -- i.e. write() just returns this value if I pass a bigger size. I
> suspect that's the TCP window size so that's why the cut-off happens.
> But where is this 50k value coming from? Can I count on it being
> consistent, or can it happen with 20k or 2k one day?
No, you cannot count on this size. Linux 2.2 had fixed-sized network
buffers (which you can control by setting a socket parameter and/or
with sysctl) but Linux 2.4 now has buffers that grow/shrink around
a suggested size, as necessary.
> And why doesn't
> select() stand up to its promise, anyway?
Because select only guarantees that you can write *something* (i.e., one
byte) to the socket, not that you can write any amount you wish to it.
Normally, sockets you select() on should all be set nonblocking, which
makes your problem go away; You select for the socket to become writable,
write however much write() lets you, and if you didn't write everything
you loop back into the select() waiting to write the rest.
--
Nadav Har'El | Thursday, Jul 11 2002, 3 Av 5762
[EMAIL PROTECTED] |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |Early bird gets the worm, but the second
http://nadav.harel.org.il |mouse gets the cheese.
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]