Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- public class TimeServer {
- // server port
- static private final int port = 6789;
- public static void main(String[] args) {
- // multicast address
- String dategroup;
- // get the multicast address in input
- dategroup = args[0];
- DatagramSocket ms = null;
- System.out.println("S> Server ready");
- try {
- // create inet address
- InetAddress ia = InetAddress.getByName(dategroup);
- byte [] data;
- long time;
- // create the datagram socket
- ms = new DatagramSocket(2000);
- // infinite cycle
- while(true) {
- // get the time
- time = System.currentTimeMillis();
- // convert the time in a String, then in byte array
- data = Long.toString(time).getBytes();
- // create the datagram packet to send
- DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);
- // send the packet
- ms.send(dp);
- // sleep a bit
- try {
- Thread.sleep(3000);
- }
- catch(InterruptedException e) {
- System.out.println("S> INTERROTTO: " + e.toString());
- }
- }
- }
- catch(IOException e) {
- System.out.println("S> ERROR: "+e.toString());
- }
- finally {
- // close datagram socket in case of exception
- if(ms != null)
- ms.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement