接收来自N个客户端的响应,以响应UDP上的广播请求 [英] Receiving response(s) from N number of clients in reply to a broadcast request over UDP

查看:136
本文介绍了接收来自N个客户端的响应,以响应UDP上的广播请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一种特定类型的网络多媒体设备实现一种IP查找程序。
我想找出LAN中所有该类型的活动设备及其IP地址和其他详细信息。

I am implementing a kind of IP finder for a particular type of network multimedia device. I want to find out all the alive devices of that type in the LAN, with their IP address and other details.

该设备具有自己的方式设备发现。

The device has its own way of device discovery.

它的工作原理如下:
客户端通过UDP通过LAN发送广播请求。

目标端口号是固定的。

作为答复,LAN中所有了解此请求格式的服务器都将响应此请求,并提供有关其自身的信息。

It works as follows: A client sends a broadcast request over the LAN via UDP.
The destination port number is fixed.
In reply, all the servers in the LAN that understand the format of this request will respond to this request providing information about themselves.

我是

现在,我的问题是我不知道有多少设备(即服务器)将响应该请求。

Now my problem is that I don't know how many devices (i.e.servers) will respond to the request.


我必须调用多少次recvfrom()?

什么时候才能知道我有

还是一般来说,recvfrom()是从多台服务器接收响应的正确选择吗?

是否有更好的解决方案(如果我是错在这里)实现相同的方式?

How many times will I have to call recvfrom()?
When will I come to know that I have handled the response from all the devices?
Or in general, is recvfrom() the right choice for receiving response from multiple servers?
Is there any better (or CORRECT if I am wrong here) way of accomplishing the same?

我正在用C / C ++编程,计划同时为Windows和Linux编写代码。

非常感谢

I am programming in C/C++, planning to code for both Windows and Linux.
Many thanks in advance.

编辑:
因此,在这里所有网络编程向导的帮助下,我找到了解决问题的方法:)

select()对我来说只是事情...

非常感谢所有花时间帮助我的人

So with the help of all the network programming wizards out here, I have found the solution to my problem :)
select() is just the thing for me...
Thanks a lot to all of you who took out time to help me

推荐答案


我必须调用recvfrom()多少次?
我什么时候才能知道我已经处理了所有设备/服务器的响应?

How many times will I have to call recvfrom()? When will I come to know that I have handled the response from all the devices/servers?

如果您不这样做,不知道设备/服务器的数量,您不知道需要调用多少次 recvfrom()或何时处理了所有响应。

If you don't know the number of devices/servers, you cannot know how many times you will need to call recvfrom() or when you've handled all the responses.

您可以考虑使用 select()循环(直到超时)并调用 recvfrom ()可以读取数据。

You might consider using a select() loop (until timeout) and call recvfrom() when data is available to read. This might be in the main thread or a separate thread.

如果数据到达的速度快于处理速度,则将丢失数据报。这将在很大程度上取决于接收到数据后解析和存储数据的速度。如果处理数据是一项繁重的操作,则可能有必要在单独的线程中进行处理或存储数据,直到接收循环超时,然后继续进行处理。

If the data arrives faster than it can be processed, you will lose datagrams. This will depend largely on the speed that the data is parsed and stored after it is received. If processing the data is an intensive operation, it may be necessary to do the processing in a separate thread or store the data until the receive loop times out and then proceed with processing it.

由于UDP不可靠,因此循环播放几次以重播应该有助于弥补部分损失,而处理则应解决重复项。

Since UDP is unreliable, looping to rebroadcast a few times should help account for some of the loss and the processing should account for duplicates.

下面的伪代码是如何我可能会解决这个问题:

The following pseudocode is how I might approach the problem:


/* get socket to receive responses */
sd = socket( ... );

do
{
    /* set receive timeout */
    timeout.tv_sec = 5;     

    /* broadcast request */
    sendto( ... );

    /* wait for responses (or timeout) */
    while(select(sd+1, &readfds, NULL, NULL, &timeout) > 0)
    {
        /* receive the response */
        recvfrom( ... );

        /* process the response (or queue for another thread / later processing) */
        ...

        /* reset receive timeout */
        timeout.tv_sec = 5; 
    }

    /* process any response queued for later (and not another thread) */

} while (necessary);




或者通常,recvfrom()是接收响应的正确选择来自多个服务器?

Or in general, is recvfrom() the right choice for receiving response from multiple servers?

recvfrom()通常与无连接模式一起使用套接字,因为它允许应用程序检索接收到的数据的源地址。

recvfrom() is commonly used with connectionless-mode sockets because it permits the application to retrieve the source address of received data.

这篇关于接收来自N个客户端的响应,以响应UDP上的广播请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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