简体   繁体   English

当通过Socket发送数据时,send / write何时返回-1?

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

Linux. Linux操作系统。 Tcp socket. Tcp套接字。

When does a send()/write() command fail? send()/ write()命令何时失败?

Does it return -1 only if the TCP send buffer in the kernel overflow? 仅当内核中的TCP发送缓冲区溢出时,它是否返回-1?

From the OpenGroup definition for write(): 从write()的OpenGroup定义:

Upon successful completion, write() [XSI] and pwrite() shall return the number of bytes actually written to the file associated with fildes. 成功完成后,write()[XSI]和pwrite()将返回实际写入与fildes关联的文件的字节数。 This number shall never be greater than nbyte. 这个数字永远不会大于nbyte。 Otherwise, -1 shall be returned and errno set to indicate the error. 否则,返回-1并设置errno以指示错误。

The same thing is done for send() - OpenGroup page for send() send()的相同事情 - send()的OpenGroup页面

You will find out more information by looking at the errno. 您将通过查看错误找到更多信息。 See the OpenGroup page on errno for some more information. 有关更多信息,请参阅errno上的OpenGroup页面。

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. 如果写入调用是非阻塞的 ,并且缓冲区已满, 则会收到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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