Get String From Stream and Can't append it in the TextArea

hi i wrote a simple network program in java and i have this problem

i when i get the data from the stream i can't append it in the TextArea

and this is the code :

/*

* Main.java

*

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package javaclient;

/**

*

* @author JAD

*/

import java.net.*;

import java.io.*;

import java.lang.Thread.*;

import java.awt.*;

import javax.swing.*;

import javax.swing.SwingUtilities;

import javax.xml.bind.ParseConversionEvent;

import java.util.*;

publicclass Mainimplements Runnable{

private javax.swing.JTextField IpAddress;

private javax.swing.JTextField Port;

private javax.swing.JTextArea Inputline;

private javax.swing.JTextArea ReciveText;

static BufferedReader in =null;

static PrintWriter out =null;

staticprivate Socket theClientSocket =null;

staticprivate String message ="";

public Main(JTextArea Inputline,JTextArea ReciveText , JTextField IpAddress,JTextField Port){

this.Port=Port;

this.Inputline=Inputline;

this.ReciveText=ReciveText;

this.IpAddress=IpAddress;

}

publicvoid Send()

{

out.println(Inputline.getText());

}

publicvoid run()

{

try{

//Instantiate a new Socket

theClientSocket =new Socket(IpAddress.getText(),Integer.parseInt(Port.getText()));

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

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

do

{

// Step 3: processing phase

try

{

// read message from server

final String message = in.readLine();

ReciveText.append(message +"\n");

}// end try

catch ( Exception ev )

{

// handle exception if error in reading server data

System.exit(1);

}// end catch

}while ( message !="TERMINATE" );

// Step 4: close connection

theClientSocket.close();

in.close();

out.close();

System.exit(1);

}// end try

catch (Exception error)

{

}// end catch

}

}

the problem is in this statment

ReciveText.append(message + "\n");

and the thing that me crazy the program exit when i send

TERMINATE

but the data doesn't appear in the TextArea

Any one here can help me

[4858 byte] By [JAD271a] at [2007-11-26 16:54:16]
# 1

At a quick glance:

1) You should really rethink your design + code

2) Don't declare the class variables as static unless you really need to, but do use the private modifier

3) You could simply pass the IP-address as a String and the port number as an int to the constructor, and initialize the socket + streams in the constructor

4) Calling System.exit(1) will halt the jvm, i.e. exit the whole program!

5) You're redeclaring the message variable! Change

final String message = in.readLine();

ReciveText.append(message + "\n");

to e.g.

message = in.readLine() + "\n";

ReciveText.append(message);

6) message != "TERMINATE" is wrong. Use !message.equals("TERMINATE") instead

7) Your nested try-catches seem a little off

There's a few to get you started.

Jukka

duckbilla at 2007-7-8 23:21:55 > top of Java-index,Core,Core APIs...
# 2
The problem has nothing to do with networking. You need to call setText() in the event-dispatching thread, via SwingUtilities.invokeLater().
ejpa at 2007-7-8 23:21:55 > top of Java-index,Core,Core APIs...