PDA

Click to See Complete Forum and Search --> : [RESOLVED] Complex loop question on a client app


Pouncer
Oct 15th, 2007, 11:35 AM
Basically, I want my client to be continously able to send messages to the server, but *also* at the same time be able to recevie and display any messages from the server. This is the loop I have in its current state on the client


while (true) {
try {
System.out.println("Enter a message to send to the server:");
nextline = stdIn.readLine();
if (nextline != null) out.println(nextline);

fromServer = in.readUTF();
if (fromServer != null) System.out.println("From server " + fromServer);

else {
out.close();
in.close();
csocket.close();
stdIn.close();
}

} catch (Exception e) { }
}


It asks me to enter a message, I enter something and it sends it to the server.. perfect. BUT it just doesnt do anything after that, it just halts. I want it to print 'enter a message' again, and also be able to print messages from the server. Hope someone can help.

ComputerJy
Oct 15th, 2007, 05:06 PM
Most likely that one of the instructions in the "else" section is throwing an exception thus the system exits the loop and the entire method.

If you are using an IDE you should use trancing tools, otherwise make use of the Exception instance to figure out whats going wrong

Pouncer
Oct 15th, 2007, 05:31 PM
Well previously I was using this which worked perfectly fine:

it just continoulsy enable me to enter something, and it sends it to the server..

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String nextline;
while ((nextline = stdIn.readLine()) != null) {
out.println(nextline);
}
out.close();
csocket.close();
stdIn.close();


But as you see from my first post, I need to change it to be able to print out strings sent from the server too..

any ideas?

ComputerJy
Oct 15th, 2007, 06:23 PM
You are not showing enough of your code so we can help.

You've got 2 choices:
1- Show more code
2- Stick to the 2nd part (http://www.vbforums.com/showpost.php?p=3030384&postcount=2)

Pouncer
Oct 15th, 2007, 06:59 PM
I sorted it;



while ((inputline = stdIn.readLine()) != null) {
out.println(inputline);

fromServer = in.readLine();
System.out.println("From server: " + fromServer);
}