-1

Okay, I know there are a lot of similar questions here, but i couldn't find out my problem by going through them. So i have to post this.Sorry for that guys. Here is my prob... I made a chat server that worked on netbeans console for output a few days ago and that worked fine. But now i am making that with the Jframes. But when i click on the send button on the frame of one client the, button freezes and nothing happens. The message doesn't goes to the other frame of the second client.

here is my client side code. I have made the thread class in the client class itself.I don't know if that would make a problem.,

public class chat extends javax.swing.JFrame {
    Socket socket;
    DataOutputStream dos  ;
chat() {
       initComponents();
}


private void sendActionPerformed(java.awt.event.ActionEvent evt) {                                     
try{
   dos = new DataOutputStream(socket.getOutputStream());

        String data ="";
        data = msg.getText();
      do{
            dos.writeUTF(msg.getText());
            dos.flush();
        }
          while(data!= "stop");

      } 
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(this, e, "Error @ Cthread1", JOptionPane.ERROR_MESSAGE);
    }
    finally{
        try{

        dos.close();
    }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this,e,"Error @Finally of Cthread1",JOptionPane.ERROR_MESSAGE);
        }
    }


}                                    

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
                  try{
           chat n = new chat();
         n.run();
         n.setVisible(true);
            }catch(Exception e){
                JOptionPane.showMessageDialog(null,e,"main",JOptionPane.ERROR_MESSAGE);
            }       
            }
    });
}                     
private javax.swing.JLabel jLabel1;
public javax.swing.JTextField msg;
private javax.swing.JButton send;              

public void run()
  {
try{

 socket = new Socket("127.0.0.1", 1036);
 dos = new DataOutputStream(socket.getOutputStream());

  Cthread2 thread2 = new Cthread2(socket);
  thread2.start();
}
 catch(Exception e){
    JOptionPane.showMessageDialog(null, e,"Error @ MAin run",      JOptionPane.ERROR_MESSAGE);
  }
}


class Cthread2 extends Thread{
  Socket socket;
Cthread2(Socket socket) {
  this.socket = socket;
 }
 public void run() {
 try {
 String fromServer ="";
     while(fromServer != "stop")
     {
         DataInputStream sr = new DataInputStream(socket.getInputStream());
        fromServer = sr.readUTF();
            msg.setText(fromServer);
     }
  } catch (IOException e) {
   JOptionPane.showMessageDialog(null,e,"Error @ Cthread2", JOptionPane.ERROR_MESSAGE);
 }
}

}

}

And here is my server class

public class Multiserver {

ServerSocket serversocket;
Socket socket;
ArrayList<Socket> al = new ArrayList<Socket>();
DataInputStream dis;
DataOutputStream dos;
Multiserver() throws IOException
{

     serversocket = new ServerSocket(1036);
     //JOptionPane.showMessageDialog(null,"Server Started on port 1036","Info !", JOptionPane.ERROR_MESSAGE);
      System.out.println("Server Started on port 1036");
    while(true)
    {

        socket = serversocket.accept();
        System.out.println(socket);
        al.add(socket);
        Pthread  thread = new Pthread(socket, al);
        thread.start();


    }

And this is the server thread

 public class Pthread extends Thread {


 DataInputStream dis;
 DataOutputStream dos;
 ArrayList al;
 Socket socket;
 Pthread(Socket socket, ArrayList al)
 {
    this.al = al;
    this.socket = socket;

 }
  public void run()
   {
    try{
        String data =new chat().msg.getText();
        dis = new DataInputStream(socket.getInputStream());
        while(data != null)
        {
            data = dis.readUTF();
            if(data != "stop")
              broadcast(data);
            else
            {
                dos = new DataOutputStream(socket.getOutputStream());
                dos.writeUTF(data);
                dos.flush();

            }
        }
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null,e,"Error @run",JOptionPane.ERROR_MESSAGE);
    }




  }

   public void broadcast(String data )

    {
    try{
    Iterator it = al.iterator();
    while(it.hasNext())
    {
       Socket socket1 = (Socket)it.next();
        dos = new DataOutputStream(socket1.getOutputStream());
        dos.writeUTF(data);
        dos.flush();


    }
    }
    catch(Exception e){
           JOptionPane.showMessageDialog(null, e,"Error @ broadcast",JOptionPane.ERROR_MESSAGE);
            }
 }


}

I tried finding out what the problem was, but i just cant seem to get it. I get stuck in when i try to run the program in my mind to understand it. Its frustrating. Thanks for anybody who could help me out with whatever the problem is. And yeah. I have tried to just change my previous program that worked on console to the one that uses frames.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kirty Bhushan
  • 147
  • 2
  • 12
  • 2
    *"Okay, I know there are a lot of similar questions here, but i couldn't find out my problem by going through them. So i have to post this.Sorry for that guys."* Do you recall hearing mention of the Event Dispatch Thread in that research? For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jan 29 '15 at 16:00

1 Answers1

2
data = msg.getText();
do{
      dos.writeUTF(msg.getText());
      dos.flush();
  }
while(data!= "stop");

= Endless loop if the first getText does not return "stop".

If you really want to do this that way, you'll have to update data inside the loop, so you evaluate against your current entry. But even that won't work. You'll flood your server because you do not wait until the msg Text has changed before sending. So if the msg contains "hello", this will send a hell of a lot greetings to your server before you can write "stop" ...

Why don't you just send one line at a time (-> no loop here)?

Also, you cannot compare String values with == or !=, use String.equals. See this high rated answer on that topic.

Also also, don't do network stuff on the EDT as Andrew so truly mentions.

Community
  • 1
  • 1
Fildor
  • 14,510
  • 4
  • 35
  • 67
  • i tried using one line at a time... that is what actually is to be used i thnk.. i used if .. but it didnt send anythng to other client ! – Kirty Bhushan Jan 29 '15 at 16:14