I have two textarea in my code.I have a connect button and on click of connect it gets connected to the server. I have a send button. When i type anything in the first textarea and click send button it should be received back and printed in the second textarea.

My problem is when i send data first time i am receiving back data properly but when i send data second time i am not receiving it back. When i send the third data i am receiving back the second data and this continues.

please help.thanks in advance This is my code.

public class send extends JFrame


  Socket s;

  int port=3000;

  String host="192.168.1.3"

  public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {

      public void run(){
        send s=new send();
        s.setvisible(true);
       }
     });

   } 

public send() {


   setDrfaultCloseOperation(EXIT_ON_CLOSE);

   setResizable(false); 

   setBounds(100,100,400,600);

   Jpanel ContentPane=new Jpanel();

   contentPane.setBorder(new EmptyBorder(5,5,5,5);

   setContentPane(contentpane);


   contentPane.setLayout(null);

   JButton connect=new Jbutton("connect");

   connect.setBounds(15,10,100,40);

   connect.addActionListener(new ActionListener(){

       public void actionPerformed(ActionEvent e{

          try{

             addr=InetAddress.getByName(host);
             s=new Socket(addr,port);
           }
          catch(UnknownHostException e1)
           {
             e1.printStackTrace();
           }  
          catch(IOException e2)
           {
             e2.printStackTrace();
           }
         }
       });
     contentPane.add(connect);

     JTextArea area=new JtextArea(); 
     area.setBounds(15,70,100,40);
     contentPane.add(area);

     JButton btn=new JButton("send"):
     btn.setBounds(15,70,170,80);
     btn.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent e)
        { 
          if(!(area.getText().trim.isEmpty())
           {
             try
              {
                PrintStream ps=new PrintStream(s.getOutputStream());
                ps.println(area.getText());
                InputStreamReader in=new InputStreamReader(s.getInputStream());
                BufferedReader br=new BufferedReader(in);

                String msg=br.readLine();
                if(msg!=null)
                  {
                    txt.append("s:"+area.getText()+"\n");
                    txt.append(msg+"\n");
                    area.setText("");
                  }
             catch(IOException e3)
                {
                    e3.printStackTrace();
                }
             }
          }
      });
     contentPane(btn);

     JTextArea txt=new JtextArea();
     txt.setBounds(15,210,290,300);
     contentPane.add(txt);


    public class send extends JFrame


      Socket s;

      int port=3000;

      String host="192.168.1.3"

    public static void main(String[] args) {

      EventQueue.invokeLater(new Runnable() {

       public void run(){
        send s=new send();
       s.setvisible(true);
       }
     });
   } 

    public send() {


       setDrfaultCloseOperation(EXIT_ON_CLOSE);

       setResizable(false); 

       setBounds(100,100,400,600);

       Jpanel ContentPane=new Jpanel();

       contentPane.setBorder(new EmptyBorder(5,5,5,5);

       setContentPane(contentpane);


       contentPane.setLayout(null);

       JButton connect=new Jbutton("connect");

       connect.setBounds(15,10,100,40);

       connect.addActionListener(new ActionListener(){

          public void actionPerformed(ActionEvent e{

            try{

               addr=InetAddress.getByName(host);
               s=new Socket(addr,port);
               }
            catch(UnknownHostException e1)
               {
                e1.printStackTrace();
               }  
            catch(IOException e2)
              {
                e2.printStackTrace();
              }
           }
        });
        contentPane.add(connect);

        JTextArea area=new JtextArea(); 
        area.setBounds(15,70,100,40);
        contentPane.add(area);

        JButton btn=new JButton("send"):
        btn.setBounds(15,70,170,80);
        btn.addActionListener(new ActionListener()
         {
          public void actionPerformed(ActionEvent e)
           { 
            if(!(area.getText().trim.isEmpty())
             {
               try
                {
                PrintStream ps=new PrintStream(s.getOutputStream());
                ps.println(area.getText());
                InputStreamReader in=new InputStreamReader(s.getInputStream());
                BufferedReader br=new BufferedReader(in);

                String msg=br.readLine();
                if(msg!=null)
                  {
                    txt.append("s:"+area.getText()+"\n");
                    txt.append(msg+"\n");
                    area.setText("");
                  }
             catch(IOException e3)
                {
                    e3.printStackTrace();
                }
             }
          }
      });
     contentPane(btn);

     JTextArea txt=new JtextArea();
     txt.setBounds(15,210,290,300);
     contentPane.add(txt);
  }

`

Opening and closing multiple streams on a single Socket can cause data loss problems (buffers discarded etc). Opening multiple streams and not closing them is even worse.
You could try creating the input & output streams just once, when you do the connect, then use those streams when sending.

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.