|
-
Oct 15th, 2007, 11:35 AM
#1
Thread Starter
Frenzied Member
[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.
-
Oct 15th, 2007, 05:06 PM
#2
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 15th, 2007, 05:31 PM
#3
Thread Starter
Frenzied Member
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?
-
Oct 15th, 2007, 06:23 PM
#4
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 15th, 2007, 06:59 PM
#5
Thread Starter
Frenzied Member
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);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|