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.