forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketTimeClient.java
More file actions
32 lines (22 loc) · 811 Bytes
/
SocketTimeClient.java
File metadata and controls
32 lines (22 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.zetcode;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
// time servers come and go; we might need to
// find a functioning server on https://www.ntppool.org/en/
public class SocketTimeClient {
public static void main(String[] args) throws IOException {
var hostname = "3.se.pool.ntp.org";
int port = 13;
try (var socket = new Socket(hostname, port)) {
try (var reader = new InputStreamReader(socket.getInputStream())) {
int character;
var output = new StringBuilder();
while ((character = reader.read()) != -1) {
output.append((char) character);
}
System.out.println(output);
}
}
}
}