Today's i have studied regarding to Time Server, RFC-868 Protocol too interesting and simple to use. The Time Protocol is a network protocol in the Internet Protocol Suit defined in 1983 in RFC 868. Its purpose is to provide a site-independent, machine readable date and time.
The Time Protocol may be implemented over the TCP and UDP. A Host connects to a server that support the Time Protocol on port 37. The server then sends the time as 32-bit unsigned integer in binary format.
Now we see that how tom implement this protocol in Blackberry application.
The Time Protocol may be implemented over the TCP and UDP. A Host connects to a server that support the Time Protocol on port 37. The server then sends the time as 32-bit unsigned integer in binary format.
Now we see that how tom implement this protocol in Blackberry application.
import java.io.DataInputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Date; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import net.rim.device.api.ui.container.MainScreen; public final class MyScreen extends MainScreen { public static final long SECONDS_1900_TO_1970 = 2208988800L; private StreamConnection connection = null; private long l = 0; private StopWatch chageLable ; public MyScreen() { try { String postfixString = "ConnectionTimeout=800;deviceside=true"; String URL = "socket://time.ien.it:37;" + postfixString; connection = (StreamConnection) Connector.open(URL); OutputStream out = connection.openOutputStream(); out.flush(); InputStream is = connection.openInputStream(); DataInputStream input; input = new DataInputStream(is); l = (input.readInt() & 0xffffffffL); } catch (Exception e) { System.out.println("Ex. "+e); } long rfcTime = new Date((l -SECONDS_1900_TO_1970)*1000L).getTime(); chageLable = new StopWatch(rfcTime); Thread t = new Thread(chageLable); t.start(); setTitle(chageLable); } }
Now Here StopWatch is LablelField and Thread so it display Time and update continuously.
import java.util.Calendar; import java.util.Date; import net.rim.device.api.i18n.SimpleDateFormat; import net.rim.device.api.system.Display; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.component.LabelField; public class StopWatch extends LabelField implements Runnable { public static int REFRESH_SCREEN = 500; private int fieldWidth = Display.getWidth(); private int fieldHeight = 30; private long rfcTime; private Calendar calTime = Calendar.getInstance(); private volatile boolean alive = true; SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss"); long diff=0; public StopWatch(long rfcTime) { this.rfcTime = rfcTime; calTime = Calendar.getInstance(); calTime.setTime(new Date(rfcTime)); diff = calTime.getTime().getTime() - new Date().getTime(); System.out.println("rfc: "+diff); } public void run() { while (alive) { rfcTime = new Date().getTime()+diff; invalidate(); try { Thread.sleep(REFRESH_SCREEN); } catch (Exception e) { } } } public int getPreferredWidth() { return fieldWidth; } public int getPreferredHeight() { return fieldHeight; } protected void layout(int arg0, int arg1) { setExtent(getPreferredWidth(), getPreferredHeight()); } protected void paint(Graphics graphics) { graphics.drawText("Time: " + df.format(new Date(rfcTime)), 0, 0); } public void invalid(){ this.invalidate(); } public long getRfcTime() { return rfcTime; } public void setRfcTime(long rfcTime) { this.rfcTime = rfcTime; } }OutPut:
References:
1. http://www.kloth.net/software/timesrv1.php
2. http://massapi.com/source/commons-net-2.2-src/src/main/java/examples/ntp/TimeClient.java.html
No comments:
Post a Comment