I've been fiddling with this for a couple days now, and just don't seem to get it to work. I have a client and a server that seem to do everything else right, and when I telnet into the server, it works as expected. But when I use the client, it goes nowhere. I've tried a number of different ways of approaching the problem, but they all produce an incorrect outcome (including a couple where one of the them loops continuously with empty data). The server spawns a thread to deal with each client. At this point, with the code below, the client sends the "START", but it is never received by the server... i.e. it is never going in to the "while ( inputLine )" loop.
Here is what I see as the relevant snippet from the server:
clientThere = true;
while ( clientThere ) {
System.err.println( "## Client is there. Trying to read from in." );
try {
while ( ( inputLine = in.readLine() ) != null ) {
System.err.println( "## in has been read: " + inputLine );
token = new StringTokenizer( inputLine.toUpperCase(), " " );
command = token.nextToken();
// If the message is "START", send a "+COMMAND" to the client
if ( command.equals( "START" ) ) {
System.err.println( "## Received: START" );
out.println( "+COMMAND " );
}
// If "BYE", send "+BYE"
else if ( command.equals( "BYE" ) ) {
System.err.println( "## Received: BYE" );
out.println( "+BYE" );
clientThere = false;
try {
mySocket.close();
} catch ( IOException e ) {
System.err.println( "IOException while closing connection: " + e );
}
}
// If "STOCK", call the getQuote() method to get stock data and send to client
else if ( command.equals( "STOCK" ) ) {
System.err.println( "## Received: STOCK" );
try {
stock = token.nextToken();
out.println( getQuote( stock ) );
} catch ( NoSuchElementException e ) {
// If the STOCK command doesn't have a stock-id, send "+ERROR: Malformed command."
out.println( "+ERROR: Malformed command." );
}
}
// Otherwise, send "+ERROR: Command not understood."
else {
System.err.println( "## Received other: " + command );
out.println( "+ERROR: Command not understood." );
}
}
} catch ( IOException e ) {
System.err.println( "Connection closed to client." );
} catch ( NoSuchElementException nsee ) {
}
}
And here is a snippet from the client:
//After receiving "+HELLO" from server...
out.println( "START" );
// Control then returns to main(), which calls upon the following code...
keyIn = new BufferedReader( new InputStreamReader( System.in ) );
// Within a loop, first read messages from the server and respond to those
// messages as described in the requirements, then read from the keyboard,
// sending the server whatever is typed by the user
while ( active ) {
try {
while ( ( serverCommand = in.readLine() ) != null ) { // Potential NullPointerException here, depending on rest of code, not currently happening.
if ( serverCommand.startsWith( "+BYE" ) ) {
System.out.println( serverCommand );
active = false;
} else if ( serverCommand.startsWith( "+COMMAND" ) ) {
System.out.print( serverCommand );
} else if ( serverCommand.startsWith( "+" ) ) {
System.out.println( serverCommand );
out.println( "START" );
} else {
System.err.println( "Something seriously fucked up happened!" );
out.println( "START" );
}
}
out.println( keyIn.readLine() );
} catch ( IOException e ) {
System.err.println( "IOException when reading from Server: " + e );
} catch ( NullPointerException npe ) {}
}
If someone can point out to me the error of my ways, I'm sure it will be a pleasant alternative to shooting myself or someone else. Part of the requirement for this class project is that we use the while ( inputLine ) loop, but even when I tried to pull this out it doesn't work correctly.
Thanx,
msd