我正在开发一个嵌入式系统,并且对这个 TCP\IP 非常陌生。我的问题是,一旦我在本地网络中安装了我的板并且该板将动态获取其 IP 地址,它必须与在网络中的一台 PC(DHCP 服务器除外)上运行的客户端应用程序通信。要与这个新板通信,客户端应用程序需要知道板的 IP 地址。有什么办法知道板子的IP地址?UDP 广播会为此目的工作吗?如果是,请详细解释,因为我无法理解。如果可能,请提供一些 C 语言示例代码。
问问题
1732 次
2 回答
1
基本思想是:
- 嵌入式系统软件打开一个 UDP 套接字,将其绑定到一个众所周知的端口,并将
SO_BROADCAST
套接字选项设置为setsockopt()
. 然后它调用recvfrom()
以在循环中等待数据包。 - 定义允许指定数据包类型的数据包格式。定义“发现”数据包类型。
- 如果嵌入式系统收到一个“发现”数据包,它会向发送者发送一个可能包含其名称/序列号/正常运行时间/状态的数据包。
- 客户端软件打开一个 UDP 套接字,设置
SO_BROADCAST
套接字选项并将“发现”类型的数据包发送到众所周知的端口和本地广播地址。 - 客户端软件等待来自每个嵌入式系统的响应
recvfrom()
,并记录每个系统的地址。 - 客户选择一个嵌入式设备并开始直接与其通信。
于 2010-05-11T13:02:44.273 回答
0
I don't know how limited your resources are but the best solution would be to include a mDNS solution like http://avahi.org/ on your board. There have specific configurations that target embedded platforms.
The beneficial part of this is that you will will be hooking into a standard mechanism for service discovery which buys you a lot if you can play well with others. Avahi is LGPL but there are other versions that are some version of BSD and ASPL(?)
于 2010-05-11T13:24:28.020 回答