具有标志MSG

编程入门 行业动态 更新时间:2024-10-10 17:31:52
本文介绍了具有标志MSG_DONTWAIT的recv | TCP套接字上的MSG_PEEK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个TCP流连接用于交换消息.这在Linux内核中.使用者线程继续处理传入的消息.消费完一条消息后,我想检查是否还有更多待处理的消息.在这种情况下,我也会处理它们.我实现此目的的代码如下所示. krecv是sock_recvmsg()的包装器,无需修改即可传递标志的值(来自 ksocket内核模块的krecv)

I have a TCP stream connection used to exchange messages. This is inside Linux kernel. The consumer thread keeps processing incoming messages. After consuming one message, I want to check if there are more pending messages; in which case I would process them too. My code to achieve this looks like below. krecv is wrapper for sock_recvmsg(), passing value of flags without modification (krecv from ksocket kernel module)

对于MSG_DONTWAIT,我希望它不应该阻止,但是显然它可以阻止.对于MSG_PEEK,如果没有要读取的数据,则应仅返回零.这种理解正确吗?有没有更好的方法来实现我在这里所需要的?我猜这应该是一个普遍的要求,因为跨节点的消息传递经常被使用.

With MSG_DONTWAIT, I am expecting it should not block, but apparently it blocks. With MSG_PEEK, if there is no data to be read, it should just return zero. Is this understanding correct ? Is there a better way to achieve what I need here ? I am guessing this should be a common requirement as message passing across nodes is used frequently.

int recvd = 0; do { recvd += krecv(*sockp, (uchar*)msg + recvd, sizeof(my_msg) - recvd, 0); printk("recvd = %d / %lu\n", recvd, sizeof(my_msg)); } while(recvd < sizeof(my_msg)); BUG_ON(recvd != sizeof(my_msg)); /* For some reason, below line _blocks_ even with no blocking flags */ recvd = krecv(*sockp, (uchar*)tempbuf, sizeof(tempbuf), MSG_PEEK | MSG_DONTWAIT); if (recvd) { printk("more data waiting to be read"); more_to_process = true; } else { printk("NO more data waiting to be read"); }

推荐答案

这是一个非常古老的问题,但是 1.问题所在 2.我面对它.

This is a very-very old question, but 1. problem persits 2. I faced with it.

至少对我来说(使用python 2.7的Ubuntu 16.04)对此MSG_DONTWAIT无效,但是,如果我将超时设置为零(使用settimeout函数),则效果很好. 可以使用setockopt函数在c语言中完成此操作.

At least for me (Ubuntu 19.04 with python 2.7) this MSG_DONTWAIT has no effect, however if I set the timeout to zero (with settimeout function), it works nicely. This can be done in c with setsockopt function.

更多推荐

具有标志MSG

本文发布于:2023-11-25 06:35:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标志   MSG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!