Hier ist ein einfacher TCP Client in Java der die aktuelle Uhrzeit von einem Timeserver abfragt und ausgibt.
import java.net.*;
import java.io.*;
public class TimeClient {
public static void main(String[] args) {
String hostname = "time.nist.gov";
int port = 13;
try (Socket socket = new Socket(hostname, port)) {
InputStream input = socket.getInputStream();
InputStreamReader reader = new InputStreamReader(input);
StringBuilder data = new StringBuilder();
int character;
while ((character = reader.read()) != -1) {
data.append((char) character);
}
System.out.println(data);
} catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}