Question

I have a problem with receive method while using multicast socket in java. there are some hosts that they want to have a group chat using multicast socket.two threads (Read & Write) are started on each host, and both of them using multicast socket which joins to a group IP. the problem is that when the receive method execute, it doesn't return the Datagram packet which has been sent before from the other host on it's write thread.what's the problem? here is some code in run method of read thread:

byte buff[]=new byte[576];
DatagramPacket DataPkt=new DatagramPacket(buff, 576);
MultiReadSocket.receive(DataPkt);
System.out.println("Datagram Packet: " + DataPkt);
ByteArrayInputStream bais = new ByteArrayInputStream(buff);
ObjectInputStream ois = new ObjectInputStream(bais);
MyPkt = (MyPacket)ois.readObject();

code in run method of write thread:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(MyPkt);
DatagramPacket DataPkt = new DatagramPacket(baos.toByteArray(),    baos.toByteArray().length,InetAddress.getByName("235.0.0.2"), 2020);
MultiWriteSocket.send(DataPkt);

if more information needed, please tell me.

Was it helpful?

Solution

As I mentioned in my comment, it's hard to be sure what's going on without seeing all of the code, but it appears that at least in the 'read' scenario, you are not joining the Multicast socket to the multicast group.

See the documentation for MulticastSocket for more info. There's a good, simple example of both writing and reading at the top of the JavaDoc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top