Java Reference
In-Depth Information
public
public final
final static
static
int
int
PORT
=
37
;
/** main: construct and run */
public
public static
static
void
void
main
(
String
[]
argv
) {
new
new
DaytimeServer
(
PORT
).
runService
();
}
/** Construct a DaytimeServer on the given port number */
public
public
DaytimeServer
(
int
int
port
) {
try
try
{
sock
=
new
new
ServerSocket
(
port
);
}
catch
catch
(
IOException e
) {
System
.
err
.
println
(
"I/O error in setup\n"
+
e
);
System
.
exit
(
1
);
}
}
/** This handles the connections */
protected
protected
void
void
runService
() {
Socket ios
=
null
null
;
DataOutputStream os
=
null
null
;
while
while
(
true
true
) {
try
try
{
System
.
out
.
println
(
"Waiting for connection on port "
+
PORT
);
ios
=
sock
.
accept
();
System
.
err
.
println
(
"Accepted from "
+
ios
.
getInetAddress
().
getHostName
());
os
=
new
new
DataOutputStream
(
ios
.
getOutputStream
());
long
long
time
=
System
.
currentTimeMillis
();
time
/=
RDateClient
.
MSEC
;
// Daytime Protocol is in seconds
// Convert to Java time base.
time
+=
RDateClient
.
BASE_DIFF
;
// Write it, truncating cast to int since it is using
// the Internet Daytime protocol which uses 4 bytes.
// This will fail in the year 2038, along with all
// 32-bit timekeeping systems based from 1970.
// Remember, you read about the Y2038 crisis here first!
os
.
writeInt
((
int
int
)
time
);
os
.
close
();
}
catch
catch
(
IOException e
) {
System
.
err
.
println
(
e
);
}
}