FileDocCategorySizeDatePackage
SocketReceive.javaAPI DocApache Tomcat 6.0.143047Fri Jul 20 04:20:36 BST 2007org.apache.catalina.tribes.test.transport

SocketReceive

public class SocketReceive extends Object

Fields Summary
static long
start
static double
mb
static byte[]
buf
static boolean
first
static int
count
static DecimalFormat
df
static BigDecimal
total
static BigDecimal
bytes
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


    
           
    
        ServerSocket srvSocket = new ServerSocket(9999);
        System.out.println("Listening on 9999");
        Socket socket = srvSocket.accept();
        socket.setReceiveBufferSize(43800);
        InputStream in = socket.getInputStream();
        Thread t = new Thread() {
            public void run() {
                while ( true ) {
                    try {
                        Thread.sleep(1000);
                        printStats(start, mb, count, df, total);
                    }catch ( Exception x ) {}
                }
            }
        };
        t.setDaemon(true);
        t.start();

        while ( true ) {
            if ( first ) { first = false; start = System.currentTimeMillis();}
            int len = in.read(buf);
            if ( len == -1 ) {
                printStats(start, mb, count, df, total);
                System.exit(1);
            }
            if ( bytes.intValue() != len ) bytes = new BigDecimal((double)len);
            total = total.add(bytes);
            mb += ( (double) len) / 1024 / 1024;
            if ( ((++count) % 10000) == 0 ) {
                printStats(start, mb, count, df, total);
            }
        }
        
    
private static voidprintStats(long start, double mb, int count, java.text.DecimalFormat df, java.math.BigDecimal total)

        long time = System.currentTimeMillis();
        double seconds = ((double)(time-start))/1000;
        System.out.println("Throughput "+df.format(mb/seconds)+" MB/seconds messages "+count+", total "+mb+" MB, total "+total+" bytes.");