Appendix C

    // Comm Link Test Program
    // Stephen Wilkinson 10/01/2000
    //

    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.util.*;
    import java.lang.*;

    public class CommTest extends Frame {
        TextArea display;
        final static int BufferSize = 2048;
        final static String Version = "1.0";
        final static String EndOfMessage = "\b";
        
        public CommTest(){
            super( "Comm Link Test " + Version );
            display = new TextArea(20, 5);
            add("Center", display);
            resize(300, 150);
            show();
        }
        
        public void runCommTest(){
            ServerSocket server;
            Socket connection;
            OutputStream output;
            DataInputStream fileInput;
            int character = 0;
            String request;
            byte [] b = new byte [BufferSize];
            
            request = new String();
            
            display.appendText("Test Program started\n");
            
            for(int count = 0; count < BufferSize; count++) {
                b[count] = 0;   
            }
            
            display.appendText("after for loop, before try\n");  
              
            try {
                display.appendText("inside try\n");
                connection = new Socket("Hex", 80);
               
                output = connection.getOutputStream();
                
                String s = new String("Sending stuff\n" + EndOfMessage);
                
                for (int i = 0; i < s.length(); i++){
                    output.write((int) s.charAt(i));   
                } 
                display.appendText("Connection made...\n");
                InputStream input = new BufferedInputStream(connection.getInputStream());
                            
               
                display.appendText("outside while\n");
                while((char)character != '#') {
               character = input.read();
               display.appendText("inside while, before if\n");
               if(character == -1){
                   display.appendText("character == -1\n");
                   return;   
             }else{
                
                 display.appendText("request = request + character\n");
                 request = request + (char)character;
                 display.appendText("\n" + request + "\n"); 
             }    
            }
                display.appendText("after while\n");
                
                output = connection.getOutputStream();
                
                s = new String("Sending other stuff" + EndOfMessage);
                int test = 0; 
                String testString = new String();
                display.appendText("s:= " + s + "\n");
                for (int i = 0; i < s.length(); i++){
                    display.appendText("inside for\n");
                    test = s.charAt(i);
                    testString = testString + (char)test;
                    display.appendText("test String " + testString);
                    output.write((int) s.charAt(i));   
                } 
                display.appendText("after sending other stuff\n");
                display.appendText("Transmission complete. Closing socket.\n");
                connection.close();
            }catch(IOException e) {
                    e.printStackTrace();
            }
        }//end of runServer
        
        public boolean handleEvent( Event e ) {
            if(e.id == Event.WINDOW_DESTROY){
                
                hide();
                dispose();
                System.exit(0);
            }
            return super.handleEvent(e);
        } //end of handleEvent
        
        public void sendData() {
            
        } // end of sendData
        
        public static void main(String args[]){
            CommTest s = new CommTest();
            s.runCommTest();
        } //end of main
    } //end of \CommTest.java
    


Back to Contents


logo - Valid XHTML 1.0!Valid CSS!Best viewed inMozilla FirefoxMozilla Firefox