HOw can i read from a socket and display message from socket in Textarea of Chatpanel even if there is no any focus in Chatpanel.I mean when i create two chatpanel instance then after clicking the send button in 1st chatpanel the message from socket should be displayed in Textarea of second chatpanel without performing any action in second chatpanel.

If both panels are running on the same machine,, then why not just have Chatpanel 1 call an update method in Chatpanel2?
If they are on different machines, then the standard solution is to implement a Listener interface for the receiver socket code. The code for Chatpanel2 should register itself as a Listener to the socket-handling code. When input arrives at the socket the code should call any/all listeners and pass the input to them so they can update themselves. This is the standard Listener pattern, as used throughout Swing.

thanx for the suggestion. When the input arrives at the socket how can i call a listener so that they can update themselves?

I've created two threads one for reading data from socket and another for writing to socket. I have called writing thread to sendButton's action in chatpanel and beacuse my project is group chat i want reading thread to run even if there is no action generated in chatpanel so that i can see what other's are sending even if i'm idle.
HOw can i do this..help....

Your reading thread sits in a loop waiting for input to arrive at its socket. When input arrives you call the Listener's method to display that input, then loops round to wait for the next input.

Can you please explain me how can i manage the reading listener

There's more than one way. Post you latest reader code so we can use that as the starting point.

//actually i've a server in python

//In chatpanel class i've done......

Runnable run1 = new Runnable() {

        public void run() {
            try {
                MainFrame.out.writeUTF(inputTextArea.getText());
                inputTextArea.setText("");
            } catch (IOException ex) {
                Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    Runnable  run2 = new Runnable() {

        public void run() {

            try {
                displayTextArea.append(MainFrame.in.readLine() + "\n");
                System.out.println("fuck");


            } catch (IOException ex) {
                Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        t1 = new Thread(run1);
        t1.start();
        t2 = new Thread(run2);// i dont know where to start this thread 
        t2.start();//that runs and message from all client is shared


    }

I don't see any Socket code there - have you started it yet? If not, just do a really simple one that just send/recieves a single line and prints it out and exits. You can incorporate that code into the real multi-threaded system when it's working.

I've socket code in MainFrame class...let me show u my code..

InetAddress serverName = address;
        // int port = serverPort;
        try {
            System.out.println("Connecting to " + serverName
                    + " on port " + serverPort);
            client = new Socket(serverName, serverPort);
            System.out.println("Just connected to "
            + client.getRemoteSocketAddress());
            outToServer = client.getOutputStream();
            out = new DataOutputStream(outToServer);


            inFromServer = client.getInputStream();
            in = new DataInputStream(inFromServer);

I want message on all client display when i click send button on one client GUI. but now i'm just getting the message after clicking the button on each individual client GUI

Sounds like you need the server to send each message to all the clients as soon as it's received?
What code do you have that uses the DataInputStream inFromServer?

InetAddress serverName = address;
        // int port = serverPort;
        try {
            System.out.println("Connecting to " + serverName
                    + " on port " + serverPort);
            client = new Socket(serverName, serverPort);
            System.out.println("Just connected to "
                    + client.getRemoteSocketAddress());
            outToServer = client.getOutputStream();
            out = new DataOutputStream(outToServer);


            inFromServer = client.getInputStream();
            in = new DataInputStream(inFromServet

This is my socket connection code in MainFrame.
I want message on display of all client's GUI when i click send button on one of the clent GUI. How can i do this

What code do you have that uses the DataInputStream inFromServer?

You just posted the code that creates it, but where do you use it? Probably something that looks like inFromServer.read...

Runnable run1 = new Runnable() {

        public void run() {//for writing to socket
            try {
                MainFrame.out.writeUTF(inputTextArea.getText());
                inputTextArea.setText("");
            } catch (IOException ex) {
                Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    Runnable  run2 = new Runnable() {//for reading from socket

        public void run() {

            try {
                displayTextArea.append(MainFrame.in.readLine() + "\n");
                System.out.println("fuck");
            } catch (IOException ex) {
                Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };

OK.
You can start your run2 thread as soon as you have a socket connection. If you put all that code into a loop it will wait on the readLine until there is input, then process it, then go round the loop to wait again for the next line (etc).
Now yopu need to have the server send any inbound messages to all the clients.

How can i run a loop in those code....i tried when i put a loop the code
Thread t1 = new Thread(run1);//wtite to socket
Thread t2 = new Thread(run2);//read from socket
t1.start();
t2.start();
how to handle this code for continous reading from socket

Just put the read into a loop within the run method - like this:

Runnable run2 = new Runnable() {//for reading from socket
boolean stop = false;
public void run() {
while (! stop) {
try {
displayTextArea.append(MainFrame.in.readLine() + "\n");
System.out.println("****");
} catch (IOException ex) {
Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};

Thanx and can i start both thread in actionlistener of send button or i do have to do in another way ...???

Like I said - start the read thread as soon as you have a socket connection, then just leave it running in its loop until you want to exit the program. That way it will respond to any input that comes in from the socket, whenever it may come in.

thanx.HOw can i test a connection existing in socket and where i can i have that test?

Thanx but how can i do that socketconnection test and where shall i do it?

You don't need to test. You have this code:

System.out.println("Connecting to " + serverName
+ " on port " + serverPort);
client = new Socket(serverName, serverPort);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
outToServer = client.getOutputStream();
out = new DataOutputStream(outToServer);

inFromServer = client.getInputStream();
in = new DataInputStream(inFromServet

so immeditately after in = new DataInputStream(... you can start thread 2 to wait for input from that data input stream

thank u a lot it really works dude!!!!!

Excellent! Mark this thread "solved" now, you can start another if you hit a new peoblem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.