您的位置:首页 > 运维架构 > Linux

linux recv 返回值 和 recv 标志参数--- 实测

2015-08-25 15:08 549 查看
==============================================================================

linux server :recv

linux client:send

send 字符‘0’、'\0'、数字0,都是可以被recv 接受到的。只是如果使用printf %s,会出现没有任何打印的现象。

即printf %s 遇 NULL停止输出。

==============================================================================

linux server :recv

linux client:send

情况:传输一次数据后,不在传输数据,然后操作client:

当client调用close关闭socket后,recv “立刻” “返回” “0”。

当client的进程被直接kill掉时(没有close),recv 同样“立刻” “返回” “0”。

当client不做任何操作时,recv阻塞,等待client发出数据信息。(1分钟以上未见recv超时报错)。

情况:recv正在接受数据,森达正在发送数据

无论client、server哪一方强制性关闭,都会导致另一方返回-1。猜测

基本上符合:

<0 出错 

=0 连接关闭 

>0 接收到数据大小

失败返回-1,errno被设为以下的某个值 :

EAGAIN:套接字已标记为非阻塞,而接收操作被阻塞或者接收超时 

EBADF:sock不是有效的描述词 

ECONNREFUSE:远程主机阻绝网络连接 

EFAULT:内存空间访问出错 

EINTR:操作被信号中断 

EINVAL:参数无效 

ENOMEM:内存不足 

ENOTCONN:与面向连接关联的套接字尚未被连接上 

ENOTSOCK:sock索引的不是套接字 当返回值是0时,为正常关闭连接;

=============================================================================

end与recv标志参数什么意思?

send(fds[i], msg, strlen(msg), MSG_NOSIGNAL);//MSG_NOSIGNAL什么意思?

 recv(sd, buf, sizeof(buf), 0);//0代表什么意思?

MSG_OOB 接收以out-of-band送出的数据;

MSG_PEEK返回来的数据并不会在系统内删除,如果再调用,recv()会返回相同的数据内容;

MSG_WAITALL强迫收到地三个参数所指定的大小的数据后才返回,除非有错误或者信号产生;

MSG_NISIGNAL此操作不愿被SIGPIPE信号中断

------解决方案--------------------

From man page:

MSG_OOB

Sends out-of-band data on sockets that support this notion (e.g. of type SOCK_STREAM); the underlying protocol must also support out-of-band data.

MSG_EOR

Terminates a record (when this notion is supported, as for sockets of type SOCK_SEQPACKET).

MSG_DONTROUTE

Don't use a gateway to send out the packet, only send to hosts on directly connected networks. This is usually used only by diagnostic or routing programs. This is only defined for protocol families that route; packet sockets don't.

MSG_DONTWAIT

Enables non-blocking operation; if the operation would block, EAGAIN is returned (this can also be enabled using the O_NONBLOCK with the F_SETFL fcntl(2)).

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.

MSG_CONFIRM (Linux 2.3+ only)

Tell the link layer that forward progress happened: you got a successful reply from the other side. If the link layer doesn't get this it'll regularly reprobe the neighbour

 (e.g. via a unicast ARP). Only valid on SOCK_DGRAM and SOCK_RAW sockets and currently only implemented for IPv4 and IPv6. See arp(7) for details.

=============================================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: