简体   繁体   中英

When data is sent over a Socket, when does send/write returns -1?

Linux. Tcp socket.

When does a send()/write() command fail?

Does it return -1 only if the TCP send buffer in the kernel overflow?

From the OpenGroup definition for write():

Upon successful completion, write() [XSI] and pwrite() shall return the number of bytes actually written to the file associated with fildes. This number shall never be greater than nbyte. Otherwise, -1 shall be returned and errno set to indicate the error.

The same thing is done for send() - OpenGroup page for send()

You will find out more information by looking at the errno. See the OpenGroup page on errno for some more information.

To your specific point - if your write call was blocking , (as is the default), an overflow would just block you until there was buffer space available.

If the write call was non-blocking , and the buffers were full, you would receive an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM