// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import static netperf.Constants.*;

public class Receiver {
    public static void test(InputStream is, int size) throws IOException {
        long t1 = System.currentTimeMillis();
        byte[] b = new byte[size];
        for(int i = 0; i < REP; i++) {
            int ix = 0;
            while(ix < size) {
                ix += is.read(b, ix, b.length - ix);
            }
            for(int j = 0; j < size; j++) if(b[j] != (byte)(size % 256)) throw new RuntimeException("Ooops");
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("size %d : %d ms\n", size, t2 - t1);
    }
    public static void main(String[] args) throws UnknownHostException, IOException {
        ServerSocket ss = new ServerSocket(12345);
        Socket s = ss.accept();
        InputStream is = s.getInputStream();
        for(int size = START_SIZE; size <= END_SIZE; size += INCR_SIZE) {
            test(is, size);
        }
        is.close();
    }
}
