multicast problem on java
multicast problem on java
- Subject: multicast problem on java
- From: Lasaro Camargos <email@hidden>
- Date: Sat, 25 Mar 2006 23:09:43 +0100
Hi everybody.
I am finding a weird behavior playing with multicast in java 1.5. I
find the same behavior on my PowerBook and on an XServer.
The problem is the following: there are two multicast groups M1 and
M2. Both bind on the same port P. When I multicast something to M1:P,
M2:P also gets it. Can you tell me whether this is the expected
behavior or a bug?
The code below reproduces the behavior.
Thanks a lot.
Lásaro.
package test;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class TestMulticast extends Thread{
String groupName1 = "239.0.0.1",
groupName2 = "239.0.0.2";
MulticastSocket msocket1,
msocket2;
DatagramSocket dsocket;
int port = 8000;
private InetAddress group1,group2;
DatagramPacket spacket,rpacket1,rpacket2;
private byte[] outbuf, inbuf1, inbuf2;
public void run() {
try {
//joining group.
msocket1 = new MulticastSocket(port);
msocket2 = new MulticastSocket(port);
group1 = InetAddress.getByName(groupName1);
group2 = InetAddress.getByName(groupName2);
msocket1.joinGroup(group1);
msocket2.joinGroup(group2);
//multicasting
outbuf = "TO BE MULTICAST TO JUST ONE GROUP".getBytes();
dsocket = new DatagramSocket();
InetAddress groupAddr = InetAddress.getByName(groupName1);
spacket = new DatagramPacket(outbuf, outbuf.length,
groupAddr, port);
dsocket.send(spacket);
//receiving
inbuf1 = new byte[outbuf.length];
inbuf2 = new byte[outbuf.length];
rpacket1 = new DatagramPacket(inbuf1, inbuf1.length);
rpacket2 = new DatagramPacket(inbuf2, inbuf2.length);
// Wait for packet
msocket1.receive(rpacket1);
System.out.println("Received 1:" + new String(inbuf1));
msocket2.receive(rpacket2);
System.out.println("Received 2:" + new String(inbuf2));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TestMulticast().start();
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden