简体   繁体   English

MulticastSocket确定接收到的消息是否为单播

[英]MulticastSocket determine if message received was unicast

My server currently is able to send and receive multicast packets. 我的服务器当前能够发送和接收多播数据包。 I am adding the ability to also receive and send unicast packets. 我添加了还可以接收和发送单播数据包的功能。 So I created "DatagramSocket uniRecv" and "DatagramSocket uniSend" to mimic the way multicast was done. 因此,我创建了“ DatagramSocket uniRecv”和“ DatagramSocket uniSend”以模仿多播的完成方式。 The problem is that when I receive a packet I need a way to tell if it is multicast or unicast. 问题是,当我收到一个数据包时,我需要一种方法来判断它是多播还是单播。 I thought "multiRecv.receive(packet)" would only work on multicast packets, but apparently it can also work on unicast packets. 我认为“ multiRecv.receive(packet)”仅适用于多播数据包,但显然也可以适用于单播数据包。 Is there a way to tell either before or after doing .receive(packet) to detect which kind of packet it is? 有没有办法在执行.receive(packet)之前或之后告诉您它是哪种数据包?

I need to know because when I send out a response it has to be done using the same method as it was received. 我需要知道,因为当我发送响应时,必须使用与接收响应相同的方法来完成。 So if I receive unicast I need to send unicast and if I receive multicast I need to send multicast. 因此,如果我收到单播,我需要发送单播,如果我收到多播,我需要发送多播。

On another note, can a MulticastSocket also send a unicast message? 另外,MulticastSocket也可以发送单播消息吗?

EDIT: Although the accepted post is true, I was able to find a work around. 编辑:虽然接受的职位是真实的,但我能够找到解决方法。 By forcing the server sending the packet to me, to use different ports for unicast and multicast, I was able to figure out which one it was by using packet.getPort(). 通过强制服务器将数据包发送给我,以便使用不同的端口进行单播和多播,我可以使用packet.getPort()来确定它是哪个端口。

I need a way to tell if it is multicast or unicast. 我需要一种方法来判断它是多播还是单播。 I thought "multiRecv.receive(packet)" would only work on multicast packets, but apparently it can also work on unicast packets. 我认为“ multiRecv.receive(packet)”仅适用于多播数据包,但显然也可以适用于单播数据包。 Is there a way to tell either before or after doing .receive(packet) to detect which kind of packet it is? 有没有办法在执行.receive(packet)之前或之后告诉您它是哪种数据包?

Not in Java. 不是用Java。 In the BSD Sockets API there is a feature to retrieve the target address of a datagram, but it isn't implemented in Java. 在BSD Sockets API中,有一个功能可以检索数据报的目标地址,但是它不是用Java实现的。

I need to know because when I send out a response it has to be done using the same method as it was received. 我需要知道,因为当我发送响应时,必须使用与接收响应相同的方法来完成。 So if I receive unicast I need to send unicast and if I receive multicast I need to send multicast. 因此,如果我收到单播,我需要发送单播,如果我收到多播,我需要发送多播。

Sorry, can't help you in Java. 抱歉,在Java中无法为您提供帮助。

On another note, can a MulticastSocket also send a unicast message? 另外,MulticastSocket也可以发送单播消息吗?

Yes. 是。 And further note that you don't need a MulticastSocket to send multicasts, and you don't need to join the group just to send either. 还要注意,您不需要MulticastSocket 发送多播,也不需要加入任何一个即可发送组播。

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

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