Re: multicast problem on java
Re: multicast problem on java
- Subject: Re: multicast problem on java
- From: Josh Graessley <email@hidden>
- Date: Mon, 27 Mar 2006 13:08:18 -0800
This is expected behavior. The network stack filters traffic based on
ports and addresses, not multicast groups. When you join a group it
tells the ethernet interface to start receiving those multicasts and
sends out an IGMP request to join the group. When those packets are
received, any sockets bound to the destination address or the any
address and the specific port will receive those packets.
Don't solve this problem by binding to multicast address. You'll run
in to another problem. When sending on a socket bound to a multicast
address, the multicast address will be used as the source address.
This will either cause the packet to get dropped in the stack or sent
with a multicast source where it will be dropped or ignored shortly
thereafter. This is a side effect of the way that multicast was
hacked in to the BSD stack.
The best solution is to get the destination address for the packet.
This is normally done using an ioctl to indicate you want that
information and recvmsg to get that information through the control
data. I have no idea how you do that stuff from Java.
-josh
On Mar 25, 2006, at 2:09 PM, Lasaro Camargos wrote:
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:
40apple.com
This email sent to email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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