4 Replies Latest reply: Feb 22, 2007 4:45 PM by EJP RSS

    chat client

    807597
      I am trying to create a simple chat client (like yahoo mssg)


      import java.io.*;
      import java.net.*;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

           
      public class chat1 {

      JTextField text;
      PrintWriter write1;
      JTextArea text1;
      BufferedReader read1;
      Socket sock;

           
           
      public static void main(String[] args)
           {
           chat1 t1= new chat1();
           t1.go();
           }
           
           
      public void go()
      {

      JFrame frame=new JFrame();
      JPanel panel=new JPanel();
      text=new JTextField(20);
      text1 = new JTextArea(15,20);
      text1.setLineWrap(true);
      text1.setWrapStyleWord(true);
      text1.setEditable(false);
      JScrollPane qscroll=new JScrollPane(text1);
      qscroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
      qscroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

      JButton button=new JButton("send");
      button.addActionListener(new hack());
      panel.add(qscroll);
      panel.add(text);
      panel.add(button);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setcon();

      Thread t=new Thread(new Myrun());
      t.start();

      frame.getContentPane().add(BorderLayout.CENTER,panel);
      frame.setSize(400,500);
      frame.setVisible(true);
      }


      private void setcon()
      {

      try{
      sock=new Socket("192.168.1.102",4242);
      InputStreamReader s1 = new InputStreamReader(sock.getInputStream());
      read1 = new BufferedReader(s1);
      write1=new PrintWriter(sock.getOutputStream());
      System.out.print("connection established");
      }
      catch(IOException ex)
      {
      ex.printStackTrace();
      }

      }


      public class hack implements ActionListener{
      public void actionPerformed(ActionEvent ev)
      {
      try{
           
      write1.println(text.getText());
      write1.flush();
      }
      catch(Exception ex)
      {
      ex.printStackTrace();
      }
      text1.setText("");
      text1.requestFocus();
      }
      }

      public class Myrun implements Runnable{
      public void run()
      {
      String a1;
      try{
      while ((a1=read1.readLine())!=null)
                     
      {
           
      System.out.print("read"+a1);
      text1.append(a1);
      }
                }
      catch(Exception ex)
      {
      ex.printStackTrace();
      }
      }
      }

      }



      ERRORS :what i am getting are-->

      java.net.ConnectException: Connection refused: connect
           at java.net.PlainSocketImpl.socketConnect(Native Method)
           at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:372)
           at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:233)
           at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
           at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:385)
           at java.net.Socket.connect(Socket.java:536)
           at java.net.Socket.connect(Socket.java:486)
           at java.net.Socket.<init>(Socket.java:394)
           at java.net.Socket.<init>(Socket.java:207)
           at chat1.setcon(chat1.java:66)
           at chat1.go(chat1.java:50)
           at chat1.main(chat1.java:26)
      java.lang.NullPointerException
           at chat1$Myrun.run(chat1.java:106)
           at java.lang.Thread.run(Thread.java:799)