网络UDP广播设计? [英] Network UDP broadcast design?

查看:102
本文介绍了网络UDP广播设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++服务器/.NET客户端应用程序,其中我的服务器(在Linux上运行c ++)广播消息以表明它对整个网络仍然有效,我的.NET程序侦听数据包并解析为获取服务器的正常运行时间.

I am working on a C++ server/.NET client applications couple in which my server (which runs the c++ on linux) broadcasts a message to show it's alive to the whole network and my .NET program listens for packets and parses to get the uptime of the server.

正如我所阅读的,要将常规UDP广播发送到广播地址,我只需要发送一个数据包到192.168.0.255(在我的情况下是192.168.2.255)或255.255.255.255.这是正确的吗?我可以使用相同的端口地址吗?还有其他需要吗?

As I have read, to send a regular UDP broadcast to the broadcast address, I simply have to send a packet to 192.168.0.255 (in my case 192.168.2.255) or 255.255.255.255. Is this right? Can I use the same port address? Are there any other necessities?

我了解以下事实:如果我的.NET程序在该特定地址上进行侦听,则有可能从C ++服务器程序以外的其他应用程序接收数据包.是否有任何在C ++服务器端签名"数据包的方法,以便我的.NET程序读取数据包的标头,并确定它(几乎)是我要查找的数据包?

I understand the fact that if my .NET program listens on that particular address it is possible to receive packets from other applications than my C++ server program. Is there any method of "signing" the packet on the C++ server-side in order for my .NET program to read the header of the packet and see that it is (almost) the one I am looking for?

推荐答案

不管您使用哪种语言,这都是我的答案:

Regardless of the language you are using, here is my answer:

关于广播IP地址,两个地址都是广播地址,但是路由器不会转发有限的广播地址(即255.255.255.255).最好使用子网定向的广播地址(192.168.2.255).

Regarding the broadcast IP addresses, both addresses are broadcast addresses but the limited broadcast address (which is 255.255.255.255) won't be forwarded by routers. It is better to use the subnet-directed broadcast address (192.168.2.255).

为了发送/接收广播地址,您需要定义广播地址(广播IP地址和端口号).例如:192.168.2.255和端口号3000.客户端应用程序(发送方)必须启用SO_BROADCAST套接字选项,如下所示:

In order to send/receive a broadcast address, you need to define your broadcast address (broadcast IP address and port number). For example: 192.168.2.255 and port number 3000. The client applications (the senders) MUST enable SO_BROADCAST socket option as follows:

int enabled = 1;
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &enabled, sizeof(enabled));

其中sockfd是套接字描述符.

where sockfd is the socket descriptor.

服务器应用程序将侦听特定的端口号(端口3000).通常,服务器将使用单播消息响应每个请求.

The server application will listen on a specific port number (port 3000). Normally, the server will respond to each request using unicast message.

只要没有应用程序在同一端口号上侦听,就不会有冲突.如果另一个应用程序正在同一端口上侦听,则服务器将不会运行,除非您启用了SO_REUSEADDRESS套接字选项.但是,如果存在冲突,则您的签名取决于您的协议(消息格式).因此,请检查消息格式,如果它不遵循您的应用程序协议定义的消息格式,则拒绝该消息.

There will be no conflict as long as no application is listening on the same port number. Your server will not run if another application is listening on the same port unless you enabled SO_REUSEADDRESS socket option. However, if there is a conflict, then your signiture is depending on your protocol (message format). So, check the message format and reject the message if it does not follow the message format defined by your application protocol.

对于客户端应用程序,接收到的数据包是单播的(除非您有其他设计).因此,这一方面没有冲突.

For client applications, the received packet is unicast (unless you have another design). So, no conflict at this side.

这篇关于网络UDP广播设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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