免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10418 | 回复: 5
打印 上一主题 下一主题

[求助] 关于recv和send中FLAG字段的含义 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-21 14:50 |只看该作者 |倒序浏览
受累打听一下这两个函数中FLAG字段的含义是什么?
我之前从perldoc.perl.org上查询说明,里面唯独没有提到FLAG的含义

FLAG的含义毕竟很广泛,像报文结构中的偏移量、协议等字段都被称作flag,在perldoc 的perlipc开头部分的描述中,IPC信号也被称作flag,在后半截描述中,连什么-T之类的参数也被称作flag——这两个函数中的flag指的到底是什么?

从前只用过tcp socket,通过sysread来读(因为可以忽视字符串长度嘛),发送时用print;
后来作简单的udp socket,用recv来读(同理),send来发——就在这里发现的这个现象,这个FLAG参数在sysread里面就不需要,所以我就猜想是不是和UDP有某种联系,可是检查了一下UDP包头结构,里面也没有特别的FLAG字段

所以我确实想不出来这个flag指的到底是什么?

论坛徽章:
0
2 [报告]
发表于 2011-01-22 15:57 |只看该作者
拜托嘛,回复我的帖子又不会敲坏你们的键盘,说点什么嘛……

论坛徽章:
0
3 [报告]
发表于 2011-01-22 16:04 |只看该作者
今天是周日,可能大家都没空吧。
我觉得是跟C语言的recv和send参数一样的吧。
测试一把就知道了
  1.        The flags argument to a recv call is formed by OR'ing one or more of the following values:

  2.        MSG_OOB
  3.               This flag requests receipt of out-of-band data that would not be received in the normal data stream.  Some protocols place expedited data at the head of the normal data queue, and thus this flag cannot be used with such protocols.

  4.        MSG_PEEK
  5.               This flag causes the receive operation to return data from the beginning of the receive queue without removing that data from the queue.  Thus, a subsequent receive call will return the same data.

  6.        MSG_WAITALL
  7.               This flag requests that the operation block until the full request is satisfied.  However, the call may still return less data than requested if a signal is caught, an error or disconnect occurs, or the next data to be received is of a different type than that returned.

  8.        MSG_TRUNC
  9.               Return the real length of the packet, even when it was longer than the passed buffer. Only valid for packet sockets.

  10.        MSG_ERRQUEUE
  11.               This flag specifies that queued errors should be received from the socket error queue.  The error is passed in an ancillary message with a type dependent on the protocol (for IPv4 IP_RECVERR).  The user should supply a buffer of sufficient size. See cmsg(3) and ip(7) for more information.  The payload of the original packet that caused the error is passed as normal data via msg_iovec.  The original destination address of the datagram that caused the error is supplied via msg_name.

  12.               For local errors, no address is passed (this can be checked with the cmsg_len member of the cmsghdr).  For error receives, the MSG_ERRQUEUE is set in the msghdr.  After an error has been passed, the pending socket error is regenerated based on the next queued error and will be passed on the next socket operation.

  13.               The error is supplied in a sock_extended_err structure:
复制代码

论坛徽章:
0
4 [报告]
发表于 2011-01-22 16:54 |只看该作者
喔~有趣,你说的没错,确实是C的参数。
我用
  1. while($client = $socket->recv($buff, 1024, MSG_PEEK)) {
  2.         ....
  3. }
复制代码
结果呈现死循环了,虽说对MSG_PEEK加个字母减个字母都能收,但是不会死循环,说明MSG_PEEK确实和C描述的一样,明白了

谢了老兄!!

PS: 还是没搞明白……为什么perl这个函数语法和C的同名函数一样,而perl却对flag完全不加说明,好像这个“一致性”是理所当然的一样……莫非……man 2里的system call Perl里都有实现,并且语法一致?!

论坛徽章:
0
5 [报告]
发表于 2011-01-22 17:14 |只看该作者
……也不是都有,虽说perl有$$,但毕竟没提供getpid(),但也确实有很多函数是一致的……哎,感谢了老兄,以后再碰上perl文档不详细的地方我会先考虑查一下man 2的!

论坛徽章:
0
6 [报告]
发表于 2011-01-22 17:23 |只看该作者
找到线索了,汗,是perldoc recv第一段倒数第二句:

This call is actually implemented in terms of recvfrom() system call.

——虽说本文没提到flags,但man 2 recvfrom里却对flags描述的清清楚楚

……汗,查个perl文档跟玩冒险解迷类游戏一样……尤其让我想起来前两天通关的《DRAWN》
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP