baum
baum

Reputation: 1029

Bidirectional UDP Multicast

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.)

Upvotes: 0

Views: 1578

Answers (2)

dbush
dbush

Reputation: 225362

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.

Upvotes: 1

user207421
user207421

Reputation: 311050

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.

Upvotes: 1

Related Questions