Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.net.Socket;
- import java.util.ArrayList;
- public class DayTimeClient {
- public static void main(String[] args) {
- ArrayList<String> clients = new ArrayList<String>();
- clients.add("time-a.nist.gov");
- clients.add("time-b.nist.gov");
- clients.add("time-c.nist.gov");
- clients.add("time-d.nist.gov");
- for (String s : clients) {
- System.out.println("Trying to connect to " + s);
- try {
- Socket socket = new Socket(s, 13);
- socket.setSoTimeout(5000);
- BufferedInputStream bin = new BufferedInputStream(socket.getInputStream());
- while (true) {
- int datum = bin.read();
- if (datum == -1)
- break;
- System.out.write(datum);
- }
- // If we reach here we read the data from the server and printed out correctly so we need to stop
- break; // stops the loop and eventually the program
- } catch (IOException ex) {
- System.out.println("Failed to get data from" +s);
- }
- } // for loop continues if there is a connection problem
- }
- }
Add Comment
Please, Sign In to add comment