Multicasting problem

Ok i have a multicasting client that sends a request to a multicast address 224.0.1.1. Can anyone please tell me why whenever i run this, either no connection at all happens, or the packet sent back to the client is just a copy of the request message the client sends:

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.text.DecimalFormat;

import java.net.MulticastSocket;

public class SntpMulti

{

public static void main(String[] args) throws IOException

{

String serverName="224.0.1.1";

final int PORT = 123;

InetAddress local = InetAddress.getLocalHost();

InetAddress address = InetAddress.getByName(serverName);

MulticastSocket ms = new MulticastSocket(PORT);

//ms.setInterface(local);

ms.setTimeToLive(255);

byte[] buf = new NtpMessage().toByteArray();

DatagramPacket packet =

new DatagramPacket(buf, buf.length, address, PORT);

ms.send(packet);

System.out.println(""+ packet.getData());

// Get response

System.out.println("NTP request sent, waiting for response...\n");

ms.joinGroup(address);

ms.receive(packet);

System.out.println(""+ packet.getData());

System.out.println(""+ packet.getSocketAddress());

System.out.println("" + packet.getLength());

// Immediately record the incoming timestamp

double destinationTimestamp =

(System.currentTimeMillis()/1000.0) + 2208988800.0;

// Process response

NtpMessage msg = new NtpMessage(packet.getData());

double roundTripDelay = (destinationTimestamp-msg.originateTimestamp) -

(msg.transmitTimestamp-msg.receiveTimestamp);

double localClockOffset =

((msg.receiveTimestamp - msg.originateTimestamp) +

(msg.transmitTimestamp - destinationTimestamp)) / 2;

ms.leaveGroup(address);

ms.close();

}

}

This is really starting tp get on my nerves, any help would be appreciated, thanks. oh and just to be clear there is nothing wrong with the formatting which comes from another class but i know it is an problem with the multicast part, (unicast works fine ) =(

[2261 byte] By [Adamski84a] at [2007-11-27 11:18:39]
# 1

Since you send the packet before joining the group your system, quite rightly, discards the packet before you join the group! You need to start a receive thread which joins the group for receive and then sent the packet in your main thread.

sabre150a at 2007-7-29 14:32:16 > top of Java-index,Core,Core APIs...
# 2

oh ok, is there any chance you could give an example, my threading is awful.?

Adamski84a at 2007-7-29 14:32:16 > top of Java-index,Core,Core APIs...
# 3

ok i just tried joining the group before sending the packet to 224.0.1.1, and still nothing happens? is this not a viable method for solving the problem?thanks for your help sabre btw =)

Message was edited by:

Adamski84

Adamski84a at 2007-7-29 14:32:16 > top of Java-index,Core,Core APIs...
# 4

> ok i just tried joining the group before sending the

> packet to 224.0.1.1, and still nothing happens? is

> this not a viable method for solving the

> problem?thanks for your help sabre btw =)

>

You need to learn about threading. If you are going to write any networking software you will need threads.

sabre150a at 2007-7-29 14:32:16 > top of Java-index,Core,Core APIs...