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

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

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值