Results 1 to 6 of 6
  1. #1
    Vortexnl is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default 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:

    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();
            }
        }
    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.

    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

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    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.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Vortexnl is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    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...

  4. #4
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    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?

  5. #5
    Vortexnl is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    As an aside, why in the holy hell are you reading only a single character at a time?

    Flash sends '\0' as a delimiter, this is the only way I can think of reading it.

  6. #6
    BlueCow is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. JOptionPane to Exit
    By Lazairus in forum Java Applets
    Replies: 0
    Last Post: 12-02-2010, 11:36 PM
  2. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 09:32 PM
  3. Process exit value = 128
    By trivektor in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-19-2008, 09:11 AM
  4. System.exit 0 or 1?
    By antgaudi in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 07:33 PM
  5. How to exit the program..
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •