非阻塞模式下网络读写接口返回值的处理

Cyassl库

https://wolfssl.com/wolfSSL/Docs-wolfssl-manual-17-8-wolfssl-api-connection-session-io.html

int CyaSSL_read(CYASSL* ssl, void* data, int sz);

Return Values:
> 0 - the number of bytes read upon success.
0 - will be returned upon failure.  This may be caused by a either a
clean (close notify alert) shutdown or just that the peer closed the
connection.  Call CyaSSL_get_error() for the specific error code.

SSL_FATAL_ERROR - will be returned upon failure when either an error
occurred or, when using non-blocking sockets, the SSL_ERROR_WANT_READ or
SSL_ERROR_WANT_WRITE error was received and and the application needs
to call CyaSSL_read() again.  Use CyaSSL_get_error() to get a specific
error code.

int CyaSSL_write(CYASSL* ssl, const void* data, int sz);

Return Values:
> 0 - the number of bytes written upon success.
0 - will be returned upon failure.  Call CyaSSL_get_error() for the specific error code.
SSL_FATAL_ERROR - will be returned upon failure when either an error
occurred or, when using non-blocking sockets, the SSL_ERROR_WANT_READ or
SSL_ERROR_WANT_WRITE error was received and and the application needs
to call CyaSSL_write() again.  Use CyaSSL_get_error() to get a specific
error code.

Openssl库

SSL_read

RETURN VALUES
       The following return values can occur:

>0  The read operation was successful; the return value is
the number of bytes actually read from the TLS/SSL connection.

0   The read operation was not successful. The reason may either
be a clean shutdown due to a "close notify" alert sent by the peer (in
which case the SSL_RECEIVED_SHUTDOWN
           flag in the ssl
shutdown state is set (see SSL_shutdown(3), SSL_set_shutdown(3)). It is
also possible, that the peer simply shut down the underlying transport
and the
           shutdown is incomplete. Call SSL_get_error() with
the return value ret to find out, whether an error occurred or the
connection was shut down cleanly
           (SSL_ERROR_ZERO_RETURN).

SSLv2 (deprecated) does not support a shutdown alert
protocol, so it can only be detected, whether the underlying connection
was closed. It cannot be checked, whether
           the closure was initiated by the peer or by something else.

<0  The read operation was not successful, because either an
error occurred or action must be taken by the calling process. Call
SSL_get_error() with the return value ret to
           find out the reason.

WARNING
       When an SSL_read() operation has to be repeated because of
SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE, it must be repeated with
the same arguments.

SSL_write

RETURN VALUES
       The following return values can occur:

>0  The write operation was successful, the return value is
the number of bytes actually written to the TLS/SSL connection.

0   The write operation was not successful. Probably the
underlying connection was closed. Call SSL_get_error() with the return
value ret to find out, whether an error
           occurred or the connection was shut down cleanly (SSL_ERROR_ZERO_RETURN).

SSLv2 (deprecated) does not support a shutdown alert
protocol, so it can only be detected, whether the underlying connection
was closed. It cannot be checked, why the
           closure happened.

<0  The write operation was not successful, because either an
error occurred or action must be taken by the calling process. Call
SSL_get_error() with the return value ret
           to find out the reason.

非加密

recv

RETURN VALUE
       These calls return the number of bytes received, or -1 if an
error occurred. The return value will be 0 when the peer has performed
an orderly shutdown.

ERRORS
       These are some standard
errors generated by the socket layer. Additional errors may be generated
and returned from the underlying protocol modules; see their manual
pages.

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.

EBADF  The argument s is an invalid descriptor.

ECONNREFUSED
              A remote host refused to allow the network connection
(typically because it is not running the requested service).

EFAULT The receive buffer pointer(s) point outside the process’s address space.

EINTR  The receive was interrupted by delivery of a signal before any data were available.

EINVAL Invalid argument passed.

ENOMEM Could not allocate memory for recvmsg().

ENOTCONN
              The socket is associated with a connection-oriented
protocol and has not been connected (see connect(2) and accept(2)).

ENOTSOCK
              The argument s does not refer to a socket.

send

RETURN VALUE
       On success, these calls return the number of characters sent.  On error, -1 is returned, and errno is set appropriately.

ERRORS
       These are some standard errors generated by the socket layer.
Additional errors may be generated and returned from the underlying
protocol modules; see their respective man-
       ual pages.

EACCES (For Unix domain sockets, which are identified by
pathname) Write permission is denied on the destination socket file, or
search permission is denied for one  of  the
              directories the path prefix. (See path_resolution(2).)

EAGAIN or EWOULDBLOCK
              The socket is marked non-blocking and the requested operation would block.

EBADF  An invalid descriptor was specified.

ECONNRESET
              Connection reset by peer.

EDESTADDRREQ
              The socket is not connection-mode, and no peer address is set.

EFAULT An invalid user space address was specified for a parameter.

EINTR  A signal occurred before any data was transmitted.

EINVAL Invalid argument passed.

EISCONN
              The connection-mode socket was connected already but a
recipient was specified.  (Now either this error is returned, or the
recipient specification is ignored.)

EMSGSIZE

The socket type requires that message be sent atomically,
and the size of the message to be sent made this impossible.

ENOBUFS
              The output queue for a network interface was full.  This
generally indicates that the interface has stopped sending, but may be
caused by transient congestion.  (Nor-
              mally, this does not occur in Linux. Packets are just silently dropped when a device queue overflows.)

ENOMEM No memory available.

ENOTCONN
              The socket is not connected, and no target has been given.

ENOTSOCK
              The argument s is not a socket.

EOPNOTSUPP
              Some bit in the flags argument is inappropriate for the socket type.

EPIPE  The local end has been shut down on a connection oriented
socket.  In this case the process will also receive a SIGPIPE unless
MSG_NOSIGNAL is set.

时间: 2024-10-13 00:15:23

非阻塞模式下网络读写接口返回值的处理的相关文章

非阻塞模式下connect 成功失败判断

将一个socket 设置成阻塞模式和非阻塞模式,使用fcntl方法,即: 设置成非阻塞模式: 先用fcntl的F_GETFL获取flags,用F_SETFL设置flags|O_NONBLOCK; 即: flags = fcntl(sockfd, F_GETFL, 0);                        //获取文件的flags值. fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);   //设置成非阻塞模式: 同时在接收和发送数据时,需要使用MS

非阻塞socket中read、write返回值

read返回值 >0   读取数据的长度 =0   接收到对端发送的FIN,表示对端的写端关闭. <0   如果errno=EINTR.收到信号并从信号处理函数返回时,慢系统调用会返回并设置errno为EINTR,应该重新调用read.  如果errno=EAGAIN.表示当前暂时没有数据可读,应该稍后读取. 其它一般表示出错. write返回值 >0   接收数据的长度 <0   如果errno=EINTR.收到信号并从信号处理函数返回时,慢系统调用会返回并设置errno为EIN

PHP多进程非阻塞模式下结合原生Mysql与单进程效率测试对比

公司在做游戏服务器合并的时候,对大批量数据表做了合并操作,难免会出现数据格式不一致问题.根据玩家反映BUG排查,是因为某个模块下日志表出现了数据格式问题导致. 目前想到的是有两种方案解决,第一种就是把所有的日志表数据修复:第二种就是把程序逻辑修改一下,保证查找格式正确. 我的做法是想把所有数据修复一下,就从线上数据库拿了7000条数据在本地修改测试,保证无误再执行下一步计划. 由于想到数据量会很大,我这里也顺便实验了一下多进程效率和单进程效率的对比.下面看下代码,有很多需要优化的地方....这里

看到关于socket非阻塞模式设置方式记录一下。

关于socket的阻塞与非阻塞模式以及它们之间的优缺点,这已经没什么可言的:我打个很简单的比方,如果你调用socket send函数时: 如果是阻塞模式下: send先比较待发送数据的长度len和套接字s的发送缓冲的长度,如果len大于s的发送缓冲区的长度,该函数返回SOCKET_ERROR:如果len小于或者等于s的发送缓冲区的长度,那么send先检查协议是否正在发送s的发送缓冲中的数据,如果是就等待协议把数据发送完,如果协议还没有开始发送s的发送缓冲中的数据或者s的发送缓冲中没有数据,那么

关于socket阻塞与非阻塞情况下的recv、send、read、write返回值(转载)

1.阻塞模式与非阻塞模式下recv的返回值各代表什么意思?有没有区别?(就我目前了解阻塞与非阻塞recv返回值没有区分,都是 <0:出错,=0:连接关闭,>0接收到数据大小,特别:返回值 <0时并且(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)的情况下认为连接是正常的,继续接收.只是阻塞模式下recv会阻塞着接收数据,非阻塞模式下如果没有数据会返回,不会阻塞着读,因此需要 循环读取 2.阻塞模式与非阻塞模式下writ

深入 CSocket 编程之阻塞和非阻塞模式

有时,花上几个小时阅读.调试.跟踪优秀的源码程序,能够更快地掌握某些技术关键点和精髓.当然,前提是对这些技术大致上有一个了解. 我通过几个采用 CSocket 类编写并基于 Client/Server (客户端 / 服务端)的网络聊天和传输文件的程序 ( 详见: 源代码参考 ) ,在调试这些程序的过程中,追踪深入至 CSocket 类核心源码 Sockcore.cpp , 对于CSocket 类的运行机制可谓是一览无遗,并且对于阻塞和非阻塞方式下的 socket 程序的编写也是稍有体会. 阅读本

关于socket阻塞与非阻塞情况下的recv、send、read、write返回值

原帖:http://blog.csdn.net/nodeathphoenix/article/details/23284157 一,管道读写规则 当没有数据可读时 O_NONBLOCK disable:read调用阻塞,即进程暂停执行,一直等到有数据来到为止. O_NONBLOCK enable:read调用返回-1,errno值为EAGAIN. 当管道满的时候 O_NONBLOCK disable: write调用阻塞,直到有进程读走数据 O_NONBLOCK enable:调用返回-1,er

【转】阻塞模式下的超时等待

http://www.cnblogs.com/khler/archive/2010/10/27/1863005.html 网上到处流传着“妙用select函数”实现超时等待的帖子,虽然不失为一种可选做法,但总有些不符合业务需要的前提条件,比如必须为非阻塞模式,select的文件描述符必须小于系统限制(一般是32)等等.但实际使用中,往往使用阻塞模式进行通讯,但又不想一直被阻塞,比如recv函数,如果等待指定时间接收不到信息,就认为超时. 使用setsockopt函数就可以达到此效果,下面一段文字

转:PHP中实现非阻塞模式

原文来自于:http://blog.csdn.net/linvo/article/details/5466046 程序非阻塞模式,这里也可以理解成并发.而并发又暂且可以分为网络请求并发 和本地并发 . 先说一下网络请求并发 理论描述 假设有一个client,程序逻辑是要请求三个不同的server,处理各自的响应.传统模型当然是顺序执行,先发送第一个请求,等待收到响应数据后再发送第二个请求,以此类推.就像是单核CPU,一次只能处理一件事,其他事情被暂时阻塞.而并发模式可以让三个server同时处理