Bidirectional UDP Multicast

心不动则不痛 提交于 2019-12-10 11:57:44

问题


I am writing a network backend for a game server.

I planned on using multicast, so that clients could bind to the server via a multicast socket. The server could then send game updates to all members of the group.

I'm wondering, however, if it's possible to do the reverse — can clients send unicast data to the server, over the same port as the multicast socket?

I've written a test program based off of the Java Tutorials (which only sends server —> client), but I was unable to adapt that to bidirectional communication. I'm getting Address already in use and Not a multicast address errors on the client.

Are my suspicions correct that such bidirectional communications are not possible using the same port? Must I use different ports (one for multicast [server —> clients], one for unicast [clients —> server])?

(I'm doing this in Java, but I'm more interested in the network-side-of-things is this possible vs. implementation-side how do I do this.)


回答1:


Yes, this is possible.

Suppose a server with IP address 192.168.1.2 wants to send multicast messages to 224.1.2.3 port 2222 and receive unicast resposes back on port 1111. On the server side, bind a datagram socket to address/port 0.0.0.0:1111. On the client side, bind the datagram socket to 0.0.0.0:2222, then register the socket for multicast group 224.1.2.3. The server has the option to specify 192.168.1.2 when binding its local port, but is not required. The client must bind to 0.0.0.0, otherwise multicast packets can't be received on Linux systems.

When the server wants to send, it specifies both the message and the IP/port of the destination. In this case, the server uses its datagram socket to send to 224.1.2.3:2222, although you can later send to a different address/port if you wish. The resulting packet has a source IP/port of 192.168.1.2:1111 and a destination IP/port of 224.1.2.3:2222.

For the client to send back to the server, it specifies both the message and the IP/port of the destination, which in this case is 192.168.1.2:1111. So the resulting packet has a source IP/port of {client_IP}:2222 and a destination port of 192.168.1.2:1111. Only one socket on the server and one socket on each client are required.




回答2:


can clients send unicast data to the server, over the same port as the multicast socket?

Yes, as long as the socket isn't bound to the multicast address. Apparently Linux requires this, but other platforms let you bind it to 0.0.0.0.

Note that what you're asking about isn't 'bidirectional multicast'. It is multicast in one direction, and unicast in the other.



来源:https://stackoverflow.com/questions/30315109/bidirectional-udp-multicast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!