Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 10th, 2012  |  syntax: None  |  size: 1.16 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public static void main(String args[]) throws IOException {
  2.     ExecutorService pool = Executors.newCachedThreadPool();
  3.     SocketStopwatch stopwatch = new SocketStopwatch(pool);
  4.     Socket s = new Socket();
  5.     stopwatch.doTimedOperation(s, new SocketStopwatch.SocketOperation() {
  6.       @Override
  7.       public void perform(Socket s) {
  8.         try {
  9.           long t1 = System.currentTimeMillis();
  10.           s.connect(new InetSocketAddress("hh.ru", 80));
  11.           System.out.println("connected");
  12.           PrintStream ps = new PrintStream(s.getOutputStream());
  13.           BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
  14.           ps.print("GET / HTTP/1.0\r\n");
  15.           ps.print("\r\n");
  16.           ps.flush();
  17.           String line = null;
  18.           while ((line = reader.readLine()) != null) {
  19.             System.out.println(line);
  20.           }
  21.           System.out.println();
  22.           System.out.println(System.currentTimeMillis() - t1);
  23.           System.out.flush();
  24.         } catch (IOException e) {
  25.           e.printStackTrace();
  26.         }
  27.       }
  28.     },
  29.     1000);
  30.     s.close();
  31.     pool.shutdown();
  32.   }