- 10
- 0
- 约6.42千字
- 约 6页
- 2016-11-22 发布于河南
- 举报
linux socket
函数返回值
(1)、阻塞模式与非阻塞模式下recv的返回值各代表什么意思?有没有区别?(就我目前了解阻塞与非阻塞recv返回值没有区分,都是 0:出错,=0:连接关闭,0接收到数据大小,特别:返回值 0时并且(errno == EINTR || errno == EAGAIN)的情况下认为连接是正常的,继续接收。只是阻塞模式下recv会阻塞着接收数据,非阻塞模式下如果没有数据会返回,不会阻塞着读,因此需要循环读取)。
#include sys/types.h
#include sys/socket.h
ssize_t recv(int s, void *buf, size_t len, int flags);
recv() 通常只用于面向连接的socket.
flags:
MSG_OOB
MSG_PEEK
MSG_WAITALL
MSG_TRUNG
MSG_ERRQUEUE
MSG_DONTWAIT: enable non-blocking operation, if there is not data to recv, -1 is return , and EAGAIN is set.
ERRORS(errno):
EBADF
ECONNREFUSED
ENOTCONN
ENOTSOCK
EAGAIN : The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received.
EINTR
EFAULT
EINVAL
ENOMEN
(2) 阻塞模式与非阻塞模式下write的返回值各代表什么意思?有没有区别?(就我目前了解阻塞与非阻塞write返回值没有区分,都是 0:出错,=0:连接关闭,0发送数据大小,特别:返回值 0时并且(errno == EINTR || errno == EAGAIN)的情况下认为连接是正常的,继续发送。只是阻塞模式下write会阻塞着发送数据,非阻塞模式下如果暂时无法发送数据会返回,不会阻塞着 write,因此需要循环发送)。
(3) 阻塞模式下read返回值 0 errno != EINTR errno != EWOULDBLOCK errno != EAGAIN时,连接异常,需要关闭,read返回值 0 (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)时表示没有数据,需要继续接收,如果返回值大于0表示接送到数据。
非阻塞模式下read返回值 0表示没有数据,= 0表示连接断开, 0表示接收到数据。
这2种模式下的返回值是不是这么理解,有没有跟详细的理解或跟准确的说明?
(4)阻塞模式与非阻塞模式下是否send返回值 0 (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)表示暂时发送失败,需要重试,如果send返回值 = 0, errno != EINTR errno != EWOULDBLOCK errno != EAGAIN时,连接异常,需要关闭,如果send返回值 0则表示发送了数据?send的返回值是否这么理解,阻塞模式与非阻塞模式下send返回值=0是否都是发送失败,还是那个模式下表示暂时不可发送,需要重发?
#include sys/types.h
#include sys/socket.h
ssize_t send(int s, const void *buf, size_t len, int flags);
it will be used only when the socket is in a connected state.
flags:
MSG_DONTWAIT
MSG_NOSIGNAL: Requests not to send SIGPIPE on errors on stream oriented sockets when the other end breaks the connection. The EPIPE error is still returned.
……
ERRORS(errno):
EAGAIN or EWOULDBLOCK: The socket is marked non-blocking and the requested operation would block.
EBADF
ECONNRESET: Connectio
原创力文档

文档评论(0)