Shane
Shane

Reputation: 2375

Is this possible with Sockets?

I have two threads communicating via DatagramSockets. I would now like a third thread to be able to listen to the communication. After reading Broadcasting to Multiple Recipients it seems that two MulticastSockets on the third thread is what I'm looking for.

However, I get a "java.net.BindException: Address already in use: Cannot bind" error when trying to bind the MulticastSockets to the same ports that the DatagramSockets are using in the first two threads.

Upvotes: 0

Views: 147

Answers (3)

BillRobertson42
BillRobertson42

Reputation: 12883

You have two threads in the same program communicating via socket? How about creating a queue for them so it is just in process communication?

Upvotes: 0

Shane
Shane

Reputation: 2375

With the help of some of the answers and comments here I have solved it by doing the following.

I changed the 2 original threads to use Multicast sockets to communicate, and the third thread now has 2 Mulitcast sockets bound to these ports so it can listen to the communication. Probably not the most beautiful solution, but it will suffice for my needs

Upvotes: 1

Perception
Perception

Reputation: 80633

Well, your getting the exception because you cannot bind multiple sockets to the same port. If you want to support both point to point and multicast then you will need to designate different ports for each. Otherwise if you are always doing multicast broadcast then you can just drop the regular datagram sockets altogether.

Upvotes: 0

Related Questions