Can't seem to understand why my while loop doesn't exit!
I'm gonna past a small piece of code that reads data from a socket server coming from flash, and then sends it back to a client, this is the code:
Code :
public void run()
{
try
{
char charBuffer[] = new char[1];
while(in.read(charBuffer,0,1) != -1)
{
StringBuffer stringBuffer = new StringBuffer(8192);
while(charBuffer[0] != '\0')
{
System.out.println("loop 2");
stringBuffer.append(charBuffer[0]);
in.read(charBuffer, 0 ,1);
}
System.out.println("Sending message");
server.sendMessage(stringBuffer.toString(),this);
}
}
catch(IOException ioe)
{
}
finally
{
killClient();
}
}
The first while loop checks if it's possible to read data, but it will return false if there is none right? That would mean that the while loop will not execute and it will kill the client, but that doesn't happen until I actually disconnect.
I added a lot of System.out.println statements, what happens is the following:
When client connects, the try block starts
When I send a message, the first while loop executes one time, then the second loop executes the amount of times equal to the amount of characters sent, then it sends the message, and that's where it ends, it never leaves the first while loop until I disconnect! And my brain is killing me! I can't figure out why it would do that...
Can someone help me with this small but annoying thing I have going on? I just can't seem to find the logic why it doesn't execute the finally block
Re: Can't seem to understand why my while loop doesn't exit!
What evidence do you have that it is getting hung up in the outer while loop? Put some print statements in the catch and the finally blocks (before the kill method) and see what happens. Something odd could be occurring.
Re: Can't seem to understand why my while loop doesn't exit!
Quote:
Originally Posted by
aussiemcgr
What evidence do you have that it is getting hung up in the outer while loop? Put some print statements in the catch and the finally blocks (before the kill method) and see what happens. Something odd could be occurring.
Putting a print statement in the catch and finally block doesn't do anything for me, it looks like it's working like it should, but I still don't know why the loop keeps looping (I would assume you would need while(true) for that...)
Re: Can't seem to understand why my while loop doesn't exit!
ok, so is it still looping the outside loop or is it just stopping after the second loop?
If it is still looping the outside loop, I would advise researching possible cases where the in.read(charBuffer,0,1) call will return a number other than -1, but provide data that you couldn't use for some reason. Or check for situations where the statement might repeat the same operation for some reason.
If it is stopping after the second loop, we need to find exactly where it is stopping.
Re: Can't seem to understand why my while loop doesn't exit!
Ok, I changed my code to this to this:
Code :
public void run()
{
System.out.println("Starting run()");
try
{
char charBuffer[] = new char[1];
while(true && (in.read(charBuffer, 0 ,1)) != -1)
{
StringBuffer stringBuffer = new StringBuffer(8192);
stringBuffer.append(charBuffer[0]);
while(charBuffer[0] != '\0')
{
in.read(charBuffer, 0 ,1);
stringBuffer.append(charBuffer[0]);
}
server.sendMessage(stringBuffer.toString(),this);
}
}
catch(IOException ioe)
{
System.out.println(ioe);
}
finally
{
killClient();
}
}
I actually put a print statement right above the creation of the StringBuffer so I could see when the while loop would start, and I put a print statement right under the second while statement (above the in.read(charBuffer, 0 ,1); )
When I send a message, the first loop executes ONCE, then the second loop executes UNTIL it finds the character '\0' , then it just sends the message. this is weird, since I would expect either:
The finally block executes because the while loop is false
or
The first loop keeps looping (this isn't the case, because I only get one printed message)
Re: Can't seem to understand why my while loop doesn't exit!
Is your InputSteam blocking? As per the API for InputStream:
Code :
This method blocks until input data is available, end of file is detected, or an exception is thrown.
Re: Can't seem to understand why my while loop doesn't exit!
This thread has been cross posted here:
Can't seem to understand why my while loop doesn't exit! - Java Forums
Although cross posting is allowed, for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules