如何在 C 中刷新 UDP 套接字的输入缓冲区? [英] How to flush Input Buffer of an UDP Socket in C?

查看:94
本文介绍了如何在 C 中刷新 UDP 套接字的输入缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C 中刷新 UDP 套接字的输入缓冲区(如果存在这样的东西)?

How to flush Input Buffer (if such thing exists at all) of an UDP Socket in C ?

我正在一个嵌入式 Linux 环境中工作,并使用 C 来创建一些本机应用程序.在同一个网络上有几台这样的嵌入式机器,当其中一台发生事件时(我们称之为吹哨人),吹哨人应该向网络广播地址发送网络消息,以便所有机器网络(包括 WHISTLE-BLOWER)知道该事件并根据它执行一些操作.顺便说一下,我正在使用 UDP 套接字...

I'm working on an embedded Linux environment and using C to create some native application. There are several of these embedded machines on the same network, and when an event occurs on one of them (lets call it the WHISTLE-BLOWER), WHISTLE-BLOWER should send a network message to the network broadcast address, so that all machines on the network (including the WHISTLE-BLOWER) knows about the event and executes some actions according to it. I'm using UDP socket by the way...

这是它的伪代码:

main
{
    startNetworkListenerThread( networkListenerFunction );

    while( not received any SIGTERM or such )
    {
        localEventInfo = checkIfTheLocalEventOccured();
        broadcastOnNetwork( localEventInfo );
    }
}

networkListenerFunction
{
    bindSocket;

    while( not SIGTERM )
    {
// THIS IS WHERE I WANT TO FLUSH THE RECV BUFFER...
        recv_data = recvfrom( socket );
        if( validate recv data )
        {
            startExecuteLocalAction;
            sleep( 5 );
            stopExecuteLocalAction;
        }
    }
}

期望并希望处理这段代码的方式是:

The way I expect and want to work this code is:

1. LOCAL_EVENT occured
2. Broadcasted LOCAL_EVENT_INFO on network
3. All machines received EVENT_INFO, including the original broadcaster
4. All machines started executing the local action, including the original broadcaster
5. All machines' network listener(thread)s are sleeping
6. Another LOCAL_EVENT2 occured
7. Since all machines' listener are sleeping, LOCAL_EVENT2 is ignored
8. All machines' network listener(thread)s are now active again
9. GO BACK TO 1 / RESTART CYCLE
RESULT = TOTAL 2 EVENTS, 1 IGNORED

它的实际工作方式是:

1. LOCAL_EVENT occured
2. Broadcasted LOCAL_EVENT_INFO on network
3. All machines received EVENT_INFO, including the original broadcaster
4. All machines started executing the local action, including the original broadcaster
5. All machines' network listener(thread)s are sleeping
6. Another LOCAL_EVENT2 occured
7. Eventhough all machines' listener are sleeping; LOCAL_EVENT2 is queued  SOMEHOW
8. All machines' network listener(thread)s are now active again
9. All machines received EVENT_INFO2 and executed local actions again, slept and reactivated
10. GO BACK TO 1 / RESTART CYCLE
RESULT = TOTAL 2 EVENTS, 0 IGNORED

tl,dr:发送到已绑定套接字的数据包/消息/UDP 广播,其父线程在交付时刻处于休眠状态;以某种方式排队/缓冲并在所述套接字上的下一个recvfrom"调用中传送.

我希望忽略那些 UDP 广播,所以我想刷新接收缓冲区(显然不是我作为参数提供给 recvfrom 方法的那个),如果它在调用 recvfrom 之前存在.我怎样才能做到这一点?或者我应该走哪条路?

I want those UDP broadcasts to be ignored so I was thinking of flushing the receive buffer (obviously not the one i'm giving as parameter to the recvfrom method) if it exists before calling recvfrom. How can I do that? or what path should I follow?

推荐答案

请注意,刷新"的概念仅适用于输出.刷新会清空缓冲区并确保其中的所有内容都已发送到其目的地.对于输入缓冲区,数据已经在其目的地.可以读取或清除输入缓冲区,但不能刷新".

Please note that the notion of "flushing" only applies to output. A flush empties the buffer and ensures everything in it was sent to its destination. Regarding an input buffer, the data is already at its destination. Input buffers can be read from or cleared out, but not "flushed".

如果您只想确保已读取输入缓冲区中的所有内容,那么您正在寻找的是非阻塞读取操作.如果你尝试它并且没有输入,它应该返回一个错误.

If you just want to make sure you have read everything in the input buffer, what you are looking for is a non-blocking read operation. If you try it and there's no input, it should return an error.

这篇关于如何在 C 中刷新 UDP 套接字的输入缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆