/** * This Java Applet tests network throughput * from any browser (i.e. Netscape 3.0) to a default http server (see * the parameter value in SpockApplet.htm) or any host supporting * the TCP discard socket (no. 9) if you loaded the applet locally. * * @author Chuck Rothauser * @version 1.0 * * email croths@roths.com * www http://www.roths.com/chuck.html */ import java.awt.*; import java.io.*; import java.net.*; import java.util.*; import java.applet.*; /* Define applet class name here */ public class SpockApplet extends Applet { String shost; InputField inputField; InfoBox infoBox; /* Define input field in panel used by awt & user! */ class InputField extends TextField { InputField() { super( 40 ); } } /* This is where I report results */ class InfoBox extends TextArea { InfoBox() { super("", 15, 40 ); setEditable( false ); setFont( new Font( "Courier", Font.PLAIN, 12 )); } void Print( String s ) { insertText( s, getText().length() ); } } /* Set up buttons & their names in the bottom of the awt panel */ class Controls extends Panel { public Controls() { add(new Button("Test")); add(new Button("Applications")); add(new Button("Host")); add(new Button("Clear")); add(new Button("Quit")); } } /** * Convert the given interger array into a byte array * @param inta Integer array * @returns A byte array */ private byte[] intaToBytea(int[] inta) { ByteArrayOutputStream temp = new ByteArrayOutputStream(inta.length); for(int x=0; x < inta.length; x++) { temp.write(inta[x]); } return(temp.toByteArray()); } /* * Create a packet (byte array) of the given length * full of zeros. * @param len The desired length */ private byte[] createNullPacket(int len) { byte[] pkt = new byte[len]; for (int x=0; x < len; x++) { pkt[x] = 0; } return(pkt); } /* This is the back-to-back packet test. It calculates throughput * and reports the results. The packet test is called when the user * clicks on the "test" button. */ public synchronized String goTest( String pksize ) throws UnknownHostException { String s = null; String b = null; String t = null; String x = null; String pkts = null; byte[] pdata; long millib, millie, xmit; /* check packet size and create a packet of appropriate size. * the default packet size is 500 octets (ethernet adds 26 * octets to this. */ int len = Integer.parseInt(pksize); int pksent = len + 26; if ((len < 38) || (len > 1492)) { s = new String( "Packet size is invalid" + "\n" ); return s; } pdata = createNullPacket(len); /* connect to host and send 500 data packets while timing the * transfer. */ try { Socket socket = new Socket( shost, 9 ); DataOutputStream dos = new DataOutputStream( socket.getOutputStream() ); float lpdata = (float) pdata.length; millib = System.currentTimeMillis(); for (int i = 0; i < 500; i++ ) { dos.write( pdata, 0, len ); } dos.flush(); millie = System.currentTimeMillis(); xmit = millie - millib; float band = (float) ( ( ( 500.0 * ( lpdata + 26.0 ) * 8.0 ) * 1000.0 ) / xmit ); x = new String( "Transmitted packet size in bytes = "+pksent+"\n"); t = new String( "Transmission time in milliseconds = "+xmit+"\n"); b = new String( "Bandwidth for this test in bps = "+band+"\n\n"); s = new String( x + t + b ); socket.close(); } catch( IOException e ) { System.out.println( "ERROR: IO Exception"+"\n" ); } finally { if( s == null || s.length() == 0 ) { s = new String( "Sorry, not logged in..."+"\n" ); } return s; } } /* allows the user to specify a new host to test throughput to. * Note that this function is valid only for the applet loaded * locally - this is part of the Java security model! */ public synchronized String goHost( String query ) throws UnknownHostException { String z = null; shost = new String( query ); if ( shost == null || shost.length() == 0 ) { shost = new String( "vger.res.utc.com" ); } z = new String( shost + "\n" ); return z; } /* This is the applications test which is called when the user clicks * on applications. A suite of back-to-back packets are sent to test * email, sceen dump, file transfer, www access, multimedia. */ public synchronized String goAppl( String pksize ) throws UnknownHostException { String s = null; String es = null; String is = null; String fs = null; String ms = null; String hs = null; String h = null; String b = null; String t = null; String x = null; String pkts = null; byte[] pdata; long millib, millie, xmit; /* int len = Integer.parseInt(sizeField.pksize); */ int len = 742; int pksent = len + 26; if (len < 0) { s = new String( "Packet size is invalid..." ); return s; } pdata = createNullPacket(len); try { Socket socket = new Socket( shost, 9 ); DataOutputStream dos = new DataOutputStream( socket.getOutputStream() ); float lpdata = (float) pdata.length; /* Email with attachment test */ millib = System.currentTimeMillis(); for (int i = 0; i < 505; i++ ) { dos.write( pdata, 0, len ); } dos.flush(); millie = System.currentTimeMillis(); xmit = millie - millib; float eband = (float) ( ( ( 505.0 * ( lpdata + 26.0 ) * 8.0 ) * 1000.0 ) / xmit ); h = new String( "****** Applications Test ******"+"\n"); x = new String( "Email with attachment (3Mbs) "+"\n"); t = new String( "Transmission time in milliseconds = "+xmit+"\n"); b = new String( "Bandwidth for this test in bps = "+eband+"\n\n"); es = new String( h + x + t + b ); /* Computer monitor (1000 x 1000 pixels & 24 bit color) test */ millib = System.currentTimeMillis(); for (int i = 0; i < 4043; i++ ) { dos.write( pdata, 0, len ); } dos.flush(); millie = System.currentTimeMillis(); xmit = millie - millib; float cband = (float) ( ( ( 4043.0 * ( lpdata + 26.0 ) * 8.0 ) * 1000.0 ) / xmit ); x = new String( "Computer Screen Image (3MBs) "+"\n"); t = new String( "Transmission time in milliseconds = "+xmit+"\n"); b = new String( "Bandwidth for this test in bps = "+cband+"\n\n"); is = new String( x + t + b ); /* File Transfer test (1MB) */ millib = System.currentTimeMillis(); for (int i = 0; i < 1348; i++ ) { dos.write( pdata, 0, len ); } dos.flush(); millie = System.currentTimeMillis(); xmit = millie - millib; float fband = (float) ( ( ( 1348.0 * ( lpdata + 26.0 ) * 8.0 ) * 1000.0 ) / xmit ); x = new String( "File transfer test (1MB) "+"\n"); t = new String( "Transmission time in milliseconds = "+xmit+"\n"); b = new String( "Bandwidth for this test in bps = "+fband+"\n\n"); fs = new String( x + t + b ); /* Multi-Media test (2Mbs) */ millib = System.currentTimeMillis(); for (int i = 0; i < 336; i++ ) { dos.write( pdata, 0, len ); } dos.flush(); millie = System.currentTimeMillis(); xmit = millie - millib; float mband = (float) ( ( ( 336.0 * ( lpdata + 26.0 ) * 8.0 ) * 1000.0 ) / xmit ); x = new String( "Multi-Media test (2Mbs - acceptable delay 50ms - 500ms) "+"\n"); t = new String( "Transmission time in milliseconds = "+xmit+"\n"); b = new String( "Bandwidth for this test in bps = "+mband+"\n\n"); ms = new String( x + t + b ); /* WWW http access test */ millib = System.currentTimeMillis(); for (int i = 0; i < 108; i++ ) { dos.write( pdata, 0, len ); } dos.flush(); millie = System.currentTimeMillis(); xmit = millie - millib; float wband = (float) ( ( ( 108.0 * ( lpdata + 26.0 ) * 8.0 ) * 1000.0 ) / xmit ); x = new String( "WWW http access test (80KB) "+"\n"); t = new String( "Transmission time in milliseconds = "+xmit+"\n"); b = new String( "Bandwidth for this test in bps = "+wband+"\n\n"); hs = new String( x + t + b ); s = new String ( es + fs + hs + is + ms ); socket.close(); } catch( IOException e ) { System.out.println( "ERROR: IO Exception" ); } finally { if( s == null || s.length() == 0 ) { s = new String( "Sorry, not logged in..."+"\n" ); } return s; } } /* activated when user selects "quit" button */ public synchronized boolean handleEvent( Event e ) { if( e.id == Event.WINDOW_DESTROY ) { System.exit( 0 ); return true; } else { return super.handleEvent( e ); } } /* handles the user's button selections and transfers control * to the appropriate object. */ public boolean action( Event evt, Object obj ) { if( evt.target instanceof Button ) { String label = ( String )obj; if( label.equals( "Quit" )) { System.exit( 0 ); } else if( label.equals( "Test" )) { try { infoBox.Print( goTest(inputField.getText())); } catch( UnknownHostException e ) { System.out.println( "ERROR: Unknown Host" ); } } else if( label.equals( "Applications" )) { try { infoBox.Print( goAppl(inputField.getText())); } catch( UnknownHostException e ) { System.out.println( "ERROR: Unknown Host" ); } } else if( label.equals( "Host" )) { try { infoBox.Print( goHost(inputField.getText())); } catch( UnknownHostException e ) { System.out.println( "ERROR: Unknown Host" ); } } else if ( label.equals( "Clear" )) { infoBox.setText( "" ); } } return true; } /* control is transferred here by the browser after it * has loaded the applet and checked it for valid code. * * the "HOST" parameter is retrieved and stored as the * default host, then the panel and its buttons are displayed. * * the applet then waits for the user to select a function! */ public void init () { shost = getParameter("HOST"); setBackground( Color.lightGray ); setLayout( new BorderLayout() ); inputField = new InputField(); add( "North", inputField ); infoBox = new InfoBox(); add( "Center", infoBox ); add( "South", new Controls() ); show(); infoBox.Print( "Default test host = "+ shost + "\n" ); infoBox.Print( "Enter packet size before testing" + "\n" ); } }