diff --git a/src/edu/nps/moves/lifi/LiFiBenchmark.java b/src/edu/nps/moves/lifi/LiFiBenchmark.java index ba310b58aa189eb160b05811eff716d47ead8dca..eff60f4db21483041efda1ed89be2c541cfe41d0 100644 --- a/src/edu/nps/moves/lifi/LiFiBenchmark.java +++ b/src/edu/nps/moves/lifi/LiFiBenchmark.java @@ -61,8 +61,7 @@ public class LiFiBenchmark public static void main(String[] args) { - LiFiBenchmark bm = new LiFiBenchmark(args); - bm.start(); + new LiFiBenchmark(args).start(); } public LiFiBenchmark(String[] args) @@ -110,16 +109,19 @@ public class LiFiBenchmark } int repsCount = reps; int discardsCount = discards; - + long readAcc; + long startms; + int read; + byte[] buff; + while (repsCount-- > 0) { - long readAcc; - long startms = System.currentTimeMillis(); + startms = System.currentTimeMillis(); try (Socket sock = new Socket(targetIp, targetPort)) { readAcc = 0l; - byte[] buff = new byte[READ_BUFFER_SIZE]; + buff = new byte[READ_BUFFER_SIZE]; while (true) { - int read = sock.getInputStream().read(buff); + read = sock.getInputStream().read(buff); if (read == -1) break; readAcc += read; @@ -140,8 +142,10 @@ public class LiFiBenchmark private void doServer() { + ServerSocket ssock = null; + try { - ServerSocket ssock = new ServerSocket(targetPort); + ssock = new ServerSocket(targetPort); System.out.println("Serving from "+Inet4Address.getLocalHost().getHostAddress()); System.out.println("Waiting on port "+targetPort); @@ -155,27 +159,46 @@ public class LiFiBenchmark catch (IOException ex) { System.err.println("Exception waiting for connections: "+ex.getMessage()); System.exit(-1); - } - } + } finally { + try { + if (ssock != null) + ssock.close(); + } catch (IOException ex) {} + } + } private void handleConnection(Socket sock) { - new Thread(() -> { + Thread thread = new Thread(() -> { byte[] ba = new byte[WRITE_BUFFER_SIZE]; int wrote=0; byte fill = 0; try { while (true) { Arrays.fill(ba, fill++); + + // The pipe will be broken by the networkRead method due to read limit + // imposed sock.getOutputStream().write(ba); wrote+=ba.length; } } catch (IOException ex) { - System.err.println("Shutting down: " + ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage()); - System.err.println("Wrote "+wrote); + String pre = "Shutting down"; + String msg = ex.getClass().getSimpleName() + ": " + ex.getLocalizedMessage(); + if (msg.contains("Broken pipe")) + System.out.print(pre); + else + System.err.println(pre + ": " + msg); + } finally { + try { + sock.close(); + } catch (IOException ex) {} + System.out.println("\nWrote "+wrote + " bytes"); } - }).start(); + }); + thread.setDaemon(true); + thread.start(); } private long handleSize(String s)