Click to See Complete Forum and Search --> : Closing I/O
MethadoneBoy
Mar 11th, 2002, 11:04 AM
Howdy :)
How does one close the input from a BufferedReader? For instance:
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)
plenderj
Mar 11th, 2002, 11:30 AM
Why not do the stuff yourself ?
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 ?
/*
*
* ***************************************
* *** ***
* *** 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 ) { }
}
MethadoneBoy
Mar 11th, 2002, 11:34 AM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.