Question

I'm writing a multicast-based messaging program in java. For flow control of the messages, I'd like each member of the group to maintain a list of all of the other members in the group. I could do this by assigning each member a separate ID that gets sent along with the data, but I'd like to differentiate them based on address.

The only problem is, I am running many instances of the program from my local machine, so they all have the same address. Each member of the group runs a listener in a separate thread that listens for messages on a certain port of the specified multicast IP, and each sender sends to that port. Would it be possible to assign a separate port for each sender? Would the receiver be able to listen on all ports?

The receiver binds to a port using:

address = InetAddress.getByName(multicastIP);
socket = new MulticastSocket(this.port);
socket.joinGroup(address);

And messages are sent using:

packet = new DatagramPacket(data, data.length, address, port);
socket.send(packet);
Était-ce utile?

La solution

I'd like to differentiate them based on address.

The only problem is, I am running many instances of the program from my local machine, so they all have the same address.

So you can't differentiate them based only on address.

Would it be possible to assign a separate port for each sender?

Yes, if they all use different sockets.

Would the receiver be able to listen on all ports?

The receiver doesn't need to listen on different ports. You only send to one port, you only need to receive on one port.

The problem with this comes if senders are also receivers. If that's the case it won't work: you will have an explosion of ports and you will basically have to multicast to all known ports, which isn't multicasting at all really. In which case you will need to devise a unique client ID, say a UUID.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top