[RESOLVED] Complex loop question on a client app
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
Code:
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.
Re: Complex loop question on a client app
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
Re: Complex loop question on a client app
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..
Code:
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?
Re: Complex loop question on a client app
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
Re: Complex loop question on a client app
I sorted it;
Code:
while ((inputline = stdIn.readLine()) != null) {
out.println(inputline);
fromServer = in.readLine();
System.out.println("From server: " + fromServer);
}