How many bytes can I write directly to a TCP socket?

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

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

write(tcp_socket, buffer, 1024);

or do I need to use multiple calls write()with fewer bytes for each of them?

+5
source share
5 answers

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

, count, , , RLIMIT_FSIZE (. setrlimit (2)), , . (. (7).)

write() , buffer , :

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;
}
+9

. TCP/IP . ( ) , , , . man- setsockopt() write().

0

, , . , , , . , .

, , , . , - .

0

, 1024

0
source

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

-1
source

All Articles