SNTPclient.java
Submitted by rac on 15 January, 2008 - 09:46.
-
package hsz.ITDP.rac.uebungen.SNTPclientServer;
-
-
import java.io.IOException;
-
import java.net.DatagramPacket;
-
import java.net.DatagramSocket;
-
import java.net.InetAddress;
-
import java.text.DecimalFormat;
-
import java.util.*;
-
-
public class SNTPclient {
-
-
/**
-
* @param args
-
*/
-
String sntpHost = "lisa.hsz-t.ch";
-
int sntpPort = 123;
-
-
try {
-
byte[] data = generatePacket();
-
ds.send( send );
-
ds.receive(recv);
-
// Immediately record the incoming timestamp
-
//double destinationTimestamp = (System.currentTimeMillis()/1000.0) + 2208988800.0;
-
byte[] recvdata = recv.getData();
-
-
long TransmitTimestampInt = 0;
-
long TransmitTimestampFrac = 0;
-
-
for(int i=0; i<4; i++)
-
{
-
short b = recvdata[40+i];
-
if((b & 0x80)==0x80) b = (short) (128 + (b & 0x7f));
-
else b = (short) b;
-
}
-
for(int i=4; i<8; i++)
-
{
-
short b = recvdata[40+i];
-
if((b & 0x80)==0x80) b = (short) (128 + (b & 0x7f));
-
else b = (short) b;
-
}
-
ds.close();
-
}
-
}
-
}
-
-
static private byte[] generatePacket()
-
{
-
//Define packet fields
-
byte LI = 0;
-
byte VN = 4;
-
byte Mode = 3;
-
short Stratum = 0;
-
byte Poll = 0;
-
byte Precision = 0;
-
double RootDelay = 0;
-
double RootDispersion = 0;
-
byte[] ReferenceIdentifier = {0, 0, 0, 0};
-
double ReferenceTimestamp = 0;
-
double OriginateTimestamp = 0;
-
double ReceiveTimestamp = 0;
-
byte[] p = new byte[48];
-
p[0] = (byte) (LI << 6 | VN << 3 | Mode);
-
p[1] = (byte) Stratum;
-
p[2] = (byte) Poll;
-
p[3] = (byte) Precision;
-
int l = (int) (RootDelay * 65536.0);
-
p[4] = (byte) ((l >> 24) & 0xFF);
-
p[5] = (byte) ((l >> 16) & 0xFF);
-
p[6] = (byte) ((l >> 8) & 0xFF);
-
p[7] = (byte) (l & 0xFF);
-
long ul = (long) (RootDispersion * 65536.0);
-
p[8] = (byte) ((ul >> 24) & 0xFF);
-
p[9] = (byte) ((ul >> 16) & 0xFF);
-
p[10] = (byte) ((ul >> 8) & 0xFF);
-
p[11] = (byte) (ul & 0xFF);
-
p[12] = ReferenceIdentifier[0];
-
p[13] = ReferenceIdentifier[1];
-
p[14] = ReferenceIdentifier[2];
-
p[15] = ReferenceIdentifier[3];
-
for(int i=0; i<8; i++)
-
{
-
p[16+i] = (byte) (ReferenceTimestamp / base);
-
short b = p[16+i];
-
if((b & 0x80)==0x80) b = (short) (128 + (b & 0x7f));
-
else b = (short) b;
-
ReferenceTimestamp = ReferenceTimestamp - (double) (b * base);
-
-
}
-
for(int i=0; i<8; i++)
-
{
-
p[24+i] = (byte) (OriginateTimestamp / base);
-
short b = p[24+i];
-
if((b & 0x80)==0x80) b = (short) (128 + (b & 0x7f));
-
else b = (short) b;
-
OriginateTimestamp = OriginateTimestamp - (double) (b * base);
-
-
}
-
for(int i=0; i<8; i++)
-
{
-
p[32+i] = (byte) (ReceiveTimestamp / base);
-
short b = p[32+i];
-
if((b & 0x80)==0x80) b = (short) (128 + (b & 0x7f));
-
else b = (short) b;
-
ReceiveTimestamp = ReceiveTimestamp - (double) (b * base);
-
-
}
-
for(int i=0; i<8; i++)
-
{
-
p[40+i] = (byte) (TransmitTimestamp / base);
-
short b = p[40+i];
-
if((b & 0x80)==0x80) b = (short) (128 + (b & 0x7f));
-
else b = (short) b;
-
TransmitTimestamp = TransmitTimestamp - (double) (b * base);
-
-
}
-
return p;
-
}
-
{
-
c.setTimeZone(UTC); //set time zone to 0-longitudinal (London Greenwich) because time from SNTP server is GMT (=UTC when no summertime)
-
integerPart = integerPart * 1000; //get milliseconds
-
float fPart = fractionalPart; //first assign long value to float
-
fPart = fPart / 4294967296L; //shift value left so only fractional part remains; division by 2^32
-
//now fPart is a number < 1
-
fPart = fPart * 1000; //get milliseconds (drop higher precision)
-
//now 0 <= fPart <= 999 (number of milliseconds)
-
long msSinceStartOfCentury = integerPart + (long)fPart; //add integer and fractional part
-
return now;
-
}
-
-
}
»
- Printer-friendly version
- Download PDF
- 2024 reads

Post new comment