Skip to content

Instantly share code, notes, and snippets.

@VuokkoVuorinnen
Created January 5, 2014 16:29
Show Gist options
  • Save VuokkoVuorinnen/8270317 to your computer and use it in GitHub Desktop.
Save VuokkoVuorinnen/8270317 to your computer and use it in GitHub Desktop.
package testapps;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class DateClient {
private static MulticastSocket multicastSocket;
private static InetAddress group;
private static byte[] buffer = new byte[256];
public static void main(String[] args) {
try {
group = InetAddress.getByName("230.0.0.1");
multicastSocket = new MulticastSocket(4446);
multicastSocket.joinGroup(group);
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, 4446);
for (int i = 0; i < 10; ++i) {
multicastSocket.receive(packet);
System.out.println(new String(packet.getData()));
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
multicastSocket.leaveGroup(group);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
multicastSocket.close();
System.out.println("Disconnected");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment