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

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

问题描述

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

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

我工作的嵌入式Linux环境,并用C来创建一些本地应用程序。有几个在同一个网络上的这些嵌入式设备,而当事件其中之一发生时(允许称之为举报人),举报人应发送网络消息到网络广播地址,这样,所有的机器上网络(包括举报人)知道有关事件,并根据它执行一些动作。 我使用的是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...

下面是伪code吧:

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;
        }
    }
}

我期待和希望的方式来解决这个code是:

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

实际的工作方式是:

The way it actually works 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. 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博士:报文/信息/ UDP广播发送到绑定已经插座,whoose父线程是在运送矩睡觉;在某种程度上排队/缓冲,并在下次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.

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

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