Anhang B: Beispielprogramm: "DayTimeClient"
import java.net.*;
import java.io.*;
public class DayTimeClient {
public static void main(String[] args) {
Socket theSocket;
String hostname = "localhost";
int port = 13;
DataInputStream inStream;
try {
theSocket = new Socket(hostname, port);
inStream = new DataInputStream(theSocket.getInputStream());
System.out.println(
"It's " + inStream.readLine() + " at " + hostname + "."
);
inStream.close();
theSocket.close();
}
catch (UnknownHostException e) {
System.err.println(e);
}
catch (IOException e) {
System.err.println(e);
}
}
}
Man beachte:
- Erst den Stream schließen, dann die Socketverbindung
- Es werden zwei Fehler behandelt: UnknownHostException und IOException