Results 1 to 6 of 6
- 02-11-2011, 03:15 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
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:
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.Java 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(); } }
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
- 02-11-2011, 03:29 PM #2
Till which point the code reaches? What does your console prints? loop 2? Sending message? Something else?
Have a habit of putting a stack trace inside the catch block to see if your code encounters any exceptions.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-11-2011, 03:31 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
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...
- 02-11-2011, 05:03 PM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
Is it possible that the Flash bit never closes the socket, in which case your read() will never return -1?
As an aside, why in the holy hell are you reading only a single character at a time?
- 02-11-2011, 05:04 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
- 02-11-2011, 08:33 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
JOptionPane to Exit
By Lazairus in forum Java AppletsReplies: 0Last Post: 12-02-2010, 10:36 PM -
How to exit the program by typing "exit"?
By Laythe in forum New To JavaReplies: 6Last Post: 08-19-2009, 08:32 PM -
Process exit value = 128
By trivektor in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 11-19-2008, 08:11 AM -
System.exit 0 or 1?
By antgaudi in forum New To JavaReplies: 3Last Post: 11-13-2008, 06:33 PM -
How to exit the program..
By coco in forum New To JavaReplies: 1Last Post: 08-01-2007, 05:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks