MulticastSocket not receiving

Discussion in 'Java' started by paul brown, Jan 22, 2004.

  1. paul brown

    paul brown Guest

    Hi

    I've got a simple MulticastSocket program which is not able to receive any
    data for some reason.

    Here's the relevant code for the listener:

    final int port = 5555;
    final String group = "225.4.5.6";
    final int ttl = 5;

    Runnable r2 = new Runnable(){
    public void run(){
    try{
    MulticastSocket r = new MulticastSocket(port);
    r.joinGroup(InetAddress.getByName(group));
    byte[] buf =
    new byte[10];
    DatagramPacket pack =
    new DatagramPacket(buf, buf.length);
    r.setSoTimeout(5000);
    pack.setLength(10);

    while(!m_pleaseStop){
    System.out.println("Trying to receive 10
    bytes...");

    try{
    r.receive(pack);
    }
    catch(SocketTimeoutException se){
    continue;
    }

    System.out.println("Received data from: " +
    pack.getAddress().toString() +
    ":" + pack.getPort() + " with
    length: " +
    pack.getLength());

    System.out.write(pack.getData(),0,pack.getLength());
    System.out.println();
    }

    r.leaveGroup(InetAddress.getByName(group));
    r.close();
    }
    catch(Throwable th){
    th.printStackTrace();
    }
    }
    };


    I also have a sender which looks like this:

    Runnable r1 = new Runnable(){
    public void run(){
    try{
    MulticastSocket s =
    new MulticastSocket();

    byte[] buf = "192.168.1.1:8888".getBytes();
    DatagramPacket pack =
    new DatagramPacket(buf, buf.length,
    InetAddress.getByName(group),
    port);

    System.out.println("Sending " +
    new String(buf));

    while(!m_pleaseStop){
    s.send(pack,(byte)ttl);

    Thread.sleep(5000);
    }

    s.close();
    }
    catch(Throwable th){
    th.printStackTrace();
    }
    }
    };


    Using a packet sniffer i can see that the packets are getting sent out but i
    never get anything back in through the receive() call.

    When i do a 'netstat -an' the 5555 port is listed under 0.0.0.0/0.0.0.0 and
    not the multicast group i'd expect (i think)...

    I'm running an DLink - 504 ADSL router which i think is Multicast capable
    (not sure if that's even relevant).

    I'm also running the MS Loopback adapter (on Win2K).

    Using JDK 1.4.2_0.2

    Has anyone got any suggestions?

    Thanks,
    Paul
    paul brown, Jan 22, 2004
    #1
    1. Advertising

  2. paul brown

    paul brown Guest

    anyone got any thoughts on this?

    i'm still strugglign with it ...

    thanks
    Paul



    "paul brown" <> wrote in message
    news:...
    > Hi
    >
    > I've got a simple MulticastSocket program which is not able to receive any
    > data for some reason.
    >
    > Here's the relevant code for the listener:
    >
    > final int port = 5555;
    > final String group = "225.4.5.6";
    > final int ttl = 5;
    >
    > Runnable r2 = new Runnable(){
    > public void run(){
    > try{
    > MulticastSocket r = new MulticastSocket(port);
    > r.joinGroup(InetAddress.getByName(group));
    > byte[] buf =
    > new byte[10];
    > DatagramPacket pack =
    > new DatagramPacket(buf, buf.length);
    > r.setSoTimeout(5000);
    > pack.setLength(10);
    >
    > while(!m_pleaseStop){
    > System.out.println("Trying to receive 10
    > bytes...");
    >
    > try{
    > r.receive(pack);
    > }
    > catch(SocketTimeoutException se){
    > continue;
    > }
    >
    > System.out.println("Received data from: "

    +
    > pack.getAddress().toString() +
    > ":" + pack.getPort() + " with
    > length: " +
    > pack.getLength());
    >
    > System.out.write(pack.getData(),0,pack.getLength());
    > System.out.println();
    > }
    >
    > r.leaveGroup(InetAddress.getByName(group));
    > r.close();
    > }
    > catch(Throwable th){
    > th.printStackTrace();
    > }
    > }
    > };
    >
    >
    > I also have a sender which looks like this:
    >
    > Runnable r1 = new Runnable(){
    > public void run(){
    > try{
    > MulticastSocket s =
    > new MulticastSocket();
    >
    > byte[] buf = "192.168.1.1:8888".getBytes();
    > DatagramPacket pack =
    > new DatagramPacket(buf, buf.length,
    > InetAddress.getByName(group),
    > port);
    >
    > System.out.println("Sending " +
    > new String(buf));
    >
    > while(!m_pleaseStop){
    > s.send(pack,(byte)ttl);
    >
    > Thread.sleep(5000);
    > }
    >
    > s.close();
    > }
    > catch(Throwable th){
    > th.printStackTrace();
    > }
    > }
    > };
    >
    >
    > Using a packet sniffer i can see that the packets are getting sent out but

    i
    > never get anything back in through the receive() call.
    >
    > When i do a 'netstat -an' the 5555 port is listed under 0.0.0.0/0.0.0.0

    and
    > not the multicast group i'd expect (i think)...
    >
    > I'm running an DLink - 504 ADSL router which i think is Multicast capable
    > (not sure if that's even relevant).
    >
    > I'm also running the MS Loopback adapter (on Win2K).
    >
    > Using JDK 1.4.2_0.2
    >
    > Has anyone got any suggestions?
    >
    > Thanks,
    > Paul
    >
    >
    >
    >
    >
    >
    paul brown, Jan 26, 2004
    #2
    1. Advertising

  3. Andrew Thompson, Jan 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rick
    Replies:
    16
    Views:
    2,475
    Knute Johnson
    Dec 8, 2005
  2. Suresh
    Replies:
    3
    Views:
    401
  3. Ale
    Replies:
    7
    Views:
    1,153
    Knute Johnson
    Jan 16, 2007
  4. CS
    Replies:
    2
    Views:
    1,317
    Esmond Pitt
    Jun 28, 2010
  5. CS
    Replies:
    3
    Views:
    580
    Kevin McMurtrie
    Jun 26, 2010
Loading...

Share This Page