Java Reference
In-Depth Information
public
public static
static final
final
long
long
BASE_DIFF
= (
BASE_DAYS
*
24
*
60
*
60
);
/** Convert from seconds to milliseconds */
public
public static
static final
final
int
int
MSEC
=
1000
;
public
public static
void
main
(
String
[]
argv
) {
String hostName
;
iif
(
argv
.
length
==
0
)
hostName
=
"localhost"
;
static
void
else
else
hostName
=
argv
[
0
];
try
try
{
Socket sock
=
new
new
Socket
(
hostName
,
TIME_PORT
);
DataInputStream is
=
new
new
BufferedInputStream
(
sock
.
getInputStream
()));
// Read 4 bytes from the network, unsigned.
// Do it yourself; there is no readUnsignedInt().
// Long is 8 bytes on Java, but we are using the
// existing time protocol, which uses 4-byte ints.
long
new
DataInputStream
(
new
long
remoteTime
= (
((
long
long
)(
is
.
readUnsignedByte
()) <<
24
) |
((
long
long
)(
is
.
readUnsignedByte
()) <<
16
) |
((
long
long
)(
is
.
readUnsignedByte
()) <<
8
) |
((
long
long
)(
is
.
readUnsignedByte
()) <<
0
));
System
.
out
.
println
(
"Remote time is "
+
remoteTime
);
System
.
out
.
println
(
"BASE_DIFF is "
+
BASE_DIFF
);
System
.
out
.
println
(
"Time diff == "
+ (
remoteTime
-
BASE_DIFF
));
Date d
=
new
new
Date
((
remoteTime
-
BASE_DIFF
) *
MSEC
);
System
.
out
.
println
(
"Time on "
+
hostName
+
" is "
+
d
.
toString
());
System
.
out
.
println
(
"Local date/time = "
+
LocalDateTime
.
now
());
}
catch
catch
(
IOException e
) {
System
.
err
.
println
(
e
);
}
}
}
Reading and Writing Serialized Data
Problem
Having connected, you wish to transfer serialized object data.