1 5 package org.h2.test.poweroff; 6 7 import java.io.DataInputStream ; 8 import java.io.IOException ; 9 import java.net.ServerSocket ; 10 import java.net.Socket ; 11 12 public class Listener implements Runnable { 13 14 public static void main(String [] args) throws Exception { 15 new Listener().test(args); 16 } 17 18 volatile int maxValue; 19 20 void test(String [] args) throws Exception { 21 int port = 9099; 22 for (int i = 0; i < args.length; i++) { 23 if (args[i].equals("-port")) { 24 port = Integer.parseInt(args[++i]); 25 } 26 } 27 listen(port); 28 } 29 30 public void run() { 31 while(true) { 32 try { 33 Thread.sleep(10000); 34 } catch(Exception e) { 35 } 37 System.out.println("Max=" + maxValue); 38 } 39 } 40 41 void listen(int port) throws Exception { 42 new Thread (this).start(); 43 ServerSocket serverSocket = new ServerSocket (port); 44 System.out.println("Listening on " + serverSocket.toString()); 45 long time; 46 maxValue = 0; 47 while (true) { 48 Socket socket = serverSocket.accept(); 49 DataInputStream in = new DataInputStream (socket.getInputStream()); 50 System.out.println("Connected"); 51 time = System.currentTimeMillis(); 52 try { 53 while (true) { 54 int value = in.readInt(); 55 if (value < 0) { 56 break; 57 } 58 maxValue = Math.max(maxValue, value); 59 } 60 } catch (IOException e) { 61 System.out.println("Closed with Exception: " + e); 62 } 63 time = System.currentTimeMillis() - time; 64 int ops = (int) (1000 * maxValue / time); 65 System.out.println("Max=" + maxValue + " operations/sec=" + ops); 66 } 67 } 68 69 } 70 | Popular Tags |