5

As the title says, is there a limit to the number of bytes that can be written at once on a connection-oriented socket?

If I want to send a buffer of, for example, 1024 bytes, can I use a

write(tcp_socket, buffer, 1024);

or should I use multiple write() calls with a lower amount of bytes for each one?

JustTrying
  • 592
  • 4
  • 9
  • 23
  • Maybe a duplicate of http://stackoverflow.com/a/12934115/1758762 – Leo Chapiro Mar 13 '13 at 11:55
  • possible duplicate of [C++ socket programming Max size of tcp/ip socket Buffer?](http://stackoverflow.com/questions/12931528/c-socket-programming-max-size-of-tcp-ip-socket-buffer) – Ken White Mar 14 '13 at 23:26

5 Answers5

12

write() does not guarantee that all bytes will be written so multiple calls to write() are required. From man write:

The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).)

write() returns the number of bytes written so a running total of the bytes written must be maintained and used as an index into buffer and to calculate the number of remaining bytes to be written:

ssize_t total_bytes_written = 0;
while (total_bytes_written != 1024)
{
    assert(total_bytes_written < 1024);
    ssize_t bytes_written = write(tcp_socket,
                                  &buffer[total_bytes_written],
                                  1024 - total_bytes_written);
    if (bytes_written == -1)
    {
        /* Report failure and exit. */
        break;
    }
    total_bytes_written += bytes_written;
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • So I can use any `size` at first, and then I've to check the return values. Is it right? – JustTrying Mar 13 '13 at 11:58
  • 1
    Why did you put that `assert()` in there? How is it possible to hit that condition? – Mike Mar 13 '13 at 12:23
  • @Mike, it is a loop invariant and I like to use them. It might look stupid in this very small snippet but I find them useful. Feel free to take it out. – hmjd Mar 13 '13 at 12:28
1

From my experience, it is better to stay in the 1024 byte limit

HCLivess
  • 1,015
  • 1
  • 13
  • 21
0

There's no inherent limit. TCP/IP will fragment and reassemble packets as required. Your system may impose (a possibly tunable) upper limit, but it is likely to be in the multi-MB range. See your manpage for setsockopt() and always check write()'s return value.

Jens
  • 69,818
  • 15
  • 125
  • 179
0

The actual amount you can write will depend on the type of socket. In general, you need to check the return value to see how many bytes were actually written. The number of bytes written can vary depend on whether the socket is in blocking mode or not.

Also, if the socket is blocking, you may not want to wait for all the data to be written in one go. You may want to write some at a time in order to be able to something else in between write operations.

SteveP
  • 18,840
  • 9
  • 47
  • 60
-3

as you can see write socket the maximum buffer size is 1048576 bytes.

Mihai8
  • 3,113
  • 1
  • 21
  • 31
  • 3
    No. Please re-read your source. That's the maximum *socket send buffer size,* on the *IBM z/TPF,* whatever that is. It is *not* the maximum amount of data you can send in a single `send()` or `write()`, which is only constrained by the maximum value of the `int` specifying the length. – user207421 Mar 14 '13 at 01:36