Results 1 to 3 of 3

Thread: Closing I/O

  1. #1

    Thread Starter
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180

    Closing I/O

    Howdy

    How does one close the input from a BufferedReader? For instance:

    Code:
    PortNumber = Text.readInt ( KeyIn );
    Print ( PortNumber );
    just allows me to enter a number but when I press Enter, the cursor merely skips to a new line instead of printing to the screen. Anyone any ideas?

    (NB: I'm using the javagently Text class and Print is another method elsewhere in the code that basically just prints the Integer)
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Why not do the stuff yourself ?

    Code:
    		int varInput = 0;
    		try {
    			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    			varInput = Integer.valueOf(in.readLine().trim()).intValue();
    		}
    		catch (IOException e) {	}
    That will read an integer.
    It works like a dream for me.
    Below is the latest code for the project. I'm off home now for the evening, so talk to you tomorrow.
    You coming up to dublin ?

    Code:
    /*
     * 
     * ***************************************
     * ***                                 ***
     * *** 2ICT1 Communications Project    ***
     * ***                                 ***
     * ***  Mark Deegan                    ***
     * ***  Nicola Ennis                   ***
     * ***  Robert Hickson                 ***
     * ***  Jamie Plenderleith             ***
     * ***                                 ***
     * ***************************************
     * ***                                 ***
     * *** Tunneling IPv6 Packets over an  ***
     * ***  IPv4 Network                   ***
     * ***                                 ***
     * ***************************************
     * 
    */
    
    import java.net.*;
    import java.io.*;
    
    public class sockTunnel
    {
    	
    	public static void main (String[] args) 
    	{					
    		
    		try {
    			if (args[0].equals("-credits")) {
    				doIntro(true);			
    			}else {
    				doIntro( false );
    			}
    		} 
    		catch (ArrayIndexOutOfBoundsException e) {
    			doIntro( false );
    		}
    			
    		int varInput = 0;
    		try {
    			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    			varInput = Integer.valueOf(in.readLine().trim()).intValue();
    		}
    		catch (IOException e) {	}
    		
    		switch (varInput) {
    			case 1: {
    					    doClientSetup();
    						break;
    				    }
    			case 2: {
    					    doServerSetup();
    						break;
    				    }
    			default:{
    						doBadSelection();
    						break;
    					}
    		}
    	}	
    	
    	
    	
    	/*  
    	 *  User I/O Related
    	 * 
    	 */	
    	private static void p (String strString) { System.out.println(strString); }
    	private static void p ()                 { p("");                         }
    	private static void doPrompt()           { System.out.print("> ");        }
    	private static void doBadSelection() {
    		p("***");
    		p("*** Invalid selection !");
    		p("***");
    		doIntro(false);
    	}
    
    	
    	
    	
    	
    	/* 
    	 *  Menu choices code
    	 * 
    	 *    Below is the code for displaying the menus to the user
    	 *    Code to handle the user input is contained elsewhere
    	 */
    	
    	private static void doIntro(boolean showCredits) {
    		if (showCredits) { doIntroCredits(); }
    		doIntroChoices();
    		doPrompt();
    	}	
    	private static void doIntroCredits() {
    		p("***************************************");
    		p("***                                 ***");
    		p("*** 2ICT1 Communications Project    ***");
    		p("***                                 ***");
    		p("***   Mark Deegan                   ***");
    		p("***   Nicola Ennis                  ***");
    		p("***   Robert Hickson                ***");
    		p("***   Jamie Plenderleith            ***");
    		p("***                                 ***");
    		p("***************************************");
    	}
    	private static void doIntroChoices() {
    		p("***************************************");
    		p("***                                 ***");
    		p("*** Would you like this system to   ***");
    		p("***  act as the client or server?   ***");
    		p("***                                 ***");
    		p("***  1) Client                      ***");
    		p("***  2) Server                      ***");
    		p("***                                 ***");
    		p("***************************************");	
    	}		
    	private static void doClientMenu() {
    		p("***************************************");
    		p("***                                 ***");
    		p("*** What hostname/ip address would  ***");
    		p("***  you like to connect to ?       ***");
    		p("***                                 ***");
    		p("*** Given in the following form;    ***");
    		p("***  hostname:port                  ***");
    		p("***                                 ***");
    		p("***************************************");
    	}
    	private static void doServerMenu() {
    		p("***************************************");
    		p("***                                 ***");
    		p("*** What TCP port would you like    ***");
    		p("***  you like to listen on ?        ***");
    		p("***  (1 - 65535)                    ***");
    		p("***                                 ***");
    		p("***************************************");
    	}
    	
    
    	/*
    	 *  Menu choice handler code
    	 *
    	 *    Below is the code for handling user input into the
    	 *    menus. This controls connecting + listening etc.
    	 */
    
    	private static void doClientSetup() {
    		p(""); doClientMenu(); doPrompt(); String varInput = "";
    		try {
    			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    			varInput = in.readLine().trim();
    		} catch (IOException e) {	}
    		
    		if (! (varInput == "") ) {
    			if (! (varInput.lastIndexOf(":") == -1) ) {
    				String hostname = "";
    				int port = -1;
    				try { hostname = varInput.substring(0, varInput.indexOf(":")); } 
    				catch (StringIndexOutOfBoundsException e) { }								
    				try { port = Integer.parseInt(varInput.substring(varInput.indexOf(":") + 1)); }
    				catch (NumberFormatException e) { }
    				
    				connect ( hostname , port );								
    				
    			} else {				
    				clearScreen();
    				p("***************************************");
    				p("*** Please enter a hostname and     ***");
    				p("***  port combination               ***");				
    				p("***************************************");
    				doClientSetup();
    			}
    			
    		} else {
    			System.out.println("varInput is empty");
    		}
    		doLoop();
    	}
    			
    	private static void doServerSetup() {
    		doServerMenu();
    		doPrompt();
    		int varInput = TextIO.getInt();
    		
    		if ((varInput >= 1) && (varInput <= 65535)) {
    			doLoop();
    		}
    		
    	}
    
    	/*  
    	 *  Handy little functions
    	 */ 
    	
    	private static void doLoop()      { while (true) { } } 	
    	private static void clearScreen() { for (int i = 0; i < 50; i++) { p(); } }	
    	private static boolean containsInvalidCharacters( String varString ) { return false; }
    	
    	
    	/*
    	 *  connect() and listen() Methods
    	 * 
    	 *   connect() will connect to a remotehost
    	 *    As parameters it is passed the hostname and port number
    	 * 
    	 *   listen() will listen for a connectio
    	 *    As a parameter this is passed the port number on which to listen
    	 */
    	
    	private static void connect ( String strHost , int port ) {
    		Socket sock          = null;
    		PrintWriter out      = null;
    		BufferedReader bRead = null;
    		String input         = "";
    
    		try {
    				
    			p("***************************************");
    			p("*** Connecting ...                  ***");
    			p("***************************************");			
    			sock = new Socket( strHost , port );
    			
    			p("***************************************");
    			p("*** Connected                       ***");
    			p("*** Now sending file ...            ***");
    			p("***************************************");			
    			out = new PrintWriter( sock.getOutputStream(), true );
    			try {
    				bRead = new BufferedReader( new FileReader( "testfile.txt" ) );
    				while ( ( input = bRead.readLine() ) != null ) {
    					out.println( input );
    				}
    				p("File sent");
    			}
    			catch ( FileNotFoundException e ) {
    				p( "***" );
    				p( "*** Could not find testfile.txt !" );
    				p( "***" );
    				doLoop();
    			}			
    			catch ( IOException e ) {
    					
    			}
    		} 
    		catch ( UnknownHostException e ) {
    			p( "Unknown host !" );
    			doLoop();
    		}
    		catch ( IOException e ) {
    				
    		}
    	}
    	
    	private static void listen ( int port ) { }
    }
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    Yeah got that working now. Still can't get the I/O for the host name of the Server for some reason... talk to you about it tomorrow. Yeah, I'll be in Dublin tomorrow so I'll see you around.
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width