Doesn't loop a while(true) !!!

I made an Client Chat applet, but for some reason it doesn't do the read/write part to the socket that happens while(true)!!!

Well, actually it doesn't even load the screen! I mean the layout...

As you can see, wrote some System.out.println() to see where it gets stuck, and gets up to "7" and stops - it writes "7" only once and it's in a while(true)!!!

Please help me...

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.io.*;

import java.net.*;

publicclass mainextends Applet{

boolean sent =false;

String senttxt =null;

TextArea maintext =new TextArea(17,50);

TextField sendtext =new TextField(40);

Button send =new Button("Send!");

publicvoid init(){

setLayout(new FlowLayout());

add(maintext);

add(sendtext);

add(send);

maintext.setEditable(false);

cmain();

}

catch(Exception e){

System.out.println(7);

}*/

}

public main(){

}

/**Construct the applet*/

privatevoid cmain(){

Socket echoSocket =null;

PrintWriter out =null;

BufferedReader in =null;

System.out.println(1);

try{

echoSocket =new Socket("127.0.0.1", 4445);

System.out.println(2);

out =new PrintWriter(echoSocket.getOutputStream(),true);

System.out.println(3);

in =new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));

System.out.println(4);

}catch (UnknownHostException e){

maintext.append("Couldn't find server - maybe offline");

System.out.println("Couldn't find server - maybe offline");

}catch (IOException e){

maintext.append("There was a problem connecting server");

System.out.println("There was a problem connecting server");

}

System.out.println(5);

BufferedReader stdIn =new BufferedReader(new StringReader("User Connected!\n"));

System.out.println(6);

try{

while (true){

System.out.println(7);

if(sent){

stdIn =new BufferedReader(new StringReader(senttxt+"\n"));

try{ out.println(stdIn.readLine());}catch (IOException e){}

senttxt =null;

sent=false;

System.out.println(8);

}

try{ maintext.append(in.readLine());}catch (IOException e){ System.out.println(9);}

}

}finally{

System.out.println(8);

out.close();

try{ in.close();}catch (IOException e){}

try{ stdIn.close();}catch (IOException e){}

try{ echoSocket.close();}catch (IOException e){}

}

}

publicboolean action(Event evt, Object arg){

if(evt.targetinstanceof Button || evt.targetinstanceof TextField){

senttxt = sendtext.getText();

sendtext.setText("");

sent=true;

returntrue;

}

returnfalse;

}

}

[6710 byte] By [MasterShin] at [2007-9-26 3:01:25]
# 1
Maybe the problem be the following: when you use the finally clause, all that is inside the try scope is executed and all that is inside the finally scope is EVER executed too. Then, the try is executed and the finally too, executing the loop only one time.
jodevan at 2007-6-29 10:59:29 > top of Java-index,Archived Forums,Java Programming...
# 2

I gess your program stops waiting in the in.readLine() on the "try { maintext.append(in.readLine()); } catch (IOException e) { System.out.println(9); }" sentence.

First time your program enters the while(true) it writes the "7", then goes directly to that sentence because sent==false. When reading something from in, it should write "7" again.

U063667 at 2007-6-29 10:59:29 > top of Java-index,Archived Forums,Java Programming...
# 3
That's true, when I shut down the ServerSocket Applet, it starts writing 7,8,9,7,8,9,7,8,9... as in the loop.But, what do I do so it won't stop if it didn't read anything?
MasterShin at 2007-6-29 10:59:29 > top of Java-index,Archived Forums,Java Programming...