syedislam

Untitled

Nov 13th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.IOException;
  3. import java.net.Socket;
  4. import java.util.ArrayList;
  5.  
  6. public class DayTimeClient {
  7.  
  8.     public static void main(String[] args) {
  9.         ArrayList<String> clients = new ArrayList<String>();
  10.         clients.add("time-a.nist.gov");
  11.         clients.add("time-b.nist.gov");
  12.         clients.add("time-c.nist.gov");
  13.         clients.add("time-d.nist.gov");
  14.  
  15.         for (String s : clients) {
  16.             System.out.println("Trying to connect to " + s);
  17.  
  18.             try {
  19.                 Socket socket = new Socket(s, 13);
  20.                 socket.setSoTimeout(5000);
  21.                 BufferedInputStream bin = new BufferedInputStream(socket.getInputStream());
  22.                 while (true) {
  23.                     int datum = bin.read();
  24.                     if (datum == -1)
  25.                         break;
  26.                     System.out.write(datum);
  27.                 }
  28.                 // If we reach here we read the data from the server and printed out correctly so we need to stop
  29.                 break; // stops the loop and eventually the program
  30.             } catch (IOException ex) {
  31.                 System.out.println("Failed to get data from" +s);
  32.             }
  33.         } // for loop continues if there is a connection problem
  34.     }
  35. }
Add Comment
Please, Sign In to add comment