title: client server chat application gui
body: <p>I am writing a simple client server chat application using tcp .
  But the messages are not forwarded to the client/server instead i get really cryptic errors in terminal like in infinite loop until I kill the process or close the application. Can anybody please suggest the changes in code.</p>
  
  <pre><code>class UIserver extends JFrame implements ActionListener {
  JTextArea textArea;
  JButton sendButton;
  JTextField textField;
  JScrollPane scrollpane ;
  DataOutputStream dos = null;
  DataInputStream dis = null;
  Scanner scanner = new Scanner(System.in);
  
  
  public UIserver(){
      this.setTitle("Server");
      this.setLayout(new FlowLayout());
  
      textArea = new JTextArea(30,50);
      textArea.setBackground(Color.white);
      textArea.setLayout(new FlowLayout());
      scrollpane = new JScrollPane(textArea);
      getContentPane().add(scrollpane, BorderLayout.CENTER);
      this.add(scrollpane);
  
      sendButton = new JButton("Send");
      this.add(sendButton);
      sendButton.addActionListener(this);
  
  
      textField = new JTextField(30);
      this.add(textField);
  
  
      this.setVisible(true);
      this.setSize(600,600);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }//end ctor
  
  public void setDataOutput(DataOutputStream dos){
      this.dos = dos;
  }
  
  public void setDataInput(DataInputStream dis){
      this.dis = dis;
  }
  
  public void getMsg(){
  
      new Thread(new Runnable(){
          public void run (){
              while(true){
                  try {
                      String msg = dis.readUTF();
                      textArea.append("From Client :- "+msg+"\n");
                  }catch(Exception e){e.printStackTrace();}       
              }//end while
          }
      }).start();
  
  }//end getmsg
  
  public void actionPerformed(ActionEvent e) {
  
      if (e.getSource() == sendButton) {
          new Thread(new Runnable(){
              public void run (){
                  while(true){
                      try{
                          String msgsend = scanner.nextLine();
                          textArea.append("To Client :- "+msgsend+"\n");
                          //dos.writeUTF(msgsend);    
                      }catch(Exception e){e.printStackTrace();}   
  
                  }//end while        
              }
          }).start();
      }//end if
  
  }//end actionPerformed
  </code></pre>
  
  <p>This part is same for both client and server </p>
  
  <p>and now main</p>
  
  <p>Server main method</p>
  
  <pre><code>    public static void main(String[] args) throws Exception {
      UIserver usi = new UIserver();
  
      ServerSocket socket = new ServerSocket(5000);
      Socket server = socket.accept();
  
      DataInputStream dis = new DataInputStream(server.getInputStream());
      DataOutputStream dos = new DataOutputStream(server.getOutputStream());
  
      usi.setDataInput(dis);
      usi.setDataOutput(dos); 
      usi.getMsg();
  }//end main
  </code></pre>
  
  <p>client main method</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      UIclient cli = new UIclient();
  
      Socket socket = new Socket("localhost",5000);
  
      DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
      DataInputStream dis = new DataInputStream(socket.getInputStream());
  
      cli.setDataInput(dis);
      cli.setDataOutput(dos);
      cli.getMsg();
  }//end main
  </code></pre>