简体   繁体   English

在同一端口上发送/接收单播和多播UDP消息的优点/缺点?

[英]Pros/Cons of sending/receiving unicast and multicast UDP message on same port?

I have written a C++ application to send/receive UDP message thorough sockets. 我已经编写了一个C ++应用程序来通过套接字发送/接收UDP消息。 The application is able to send/receive both multicast and unicast messages. 该应用程序能够发送/接收多播和单播消息。

I want to know, is it OK to use single port for both multicast and unicast messages? 我想知道,将单个端口用于多播和单播消息是否可以? OR is it better to have separate ports for unicast and multicast messages? 还是为单播和多播消息设置单独的端口更好?

I am expecting approx 100+ messages per second including both unicast and multicast. 我期望每秒大约100条以上的消息,包括单播和多播。

Should be fine. 应该没事。 Just don't bind to the multicast address or the network interface address (use 0.0.0.0:port) and don't connect to a remote address. 只是不要绑定到多播地址或网络接口地址(使用0.0.0.0:port),也不要连接到远程地址。 You will see all traffic that goes to this port. 您将看到去往该端口的所有流量。

As long as you have only one socket, there is no problem receiving both unicast and multicast traffic. 只要只有一个套接字,接收单播和多播通信就不会有问题。

If on the other hand you have two sockets open on the same port, you might see some inconsistent behavior with unicast. 另一方面,如果在同一端口上打开了两个套接字,则单播可能会出现一些不一致的行为。

With two sockets on the same port, any multicast traffic that arrives on that point will be sent to both sockets. 在同一端口上有两个套接字的情况下,到达该点的所有多播流量都将发送到两个套接字。 However, incoming unicast traffic with either appear on both sockets or on one of the two sockets randomly. 但是,传入的单播流量要么随机出现在两个套接字上,要么随机出现在两个套接字之一上。 The exact behavior depends on the OS in question. 确切的行为取决于所讨论的操作系统。

Since you want to receive both unicast and multicast, use a single socket. 由于要同时接收单播和多播,因此请使用单个套接字。 Don't use multiple sockets on the same port unless you're working with multicast only . 除非使用多播,否则请勿在同一端口上使用多个套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM