简体   繁体   中英

sending and receiving data over socket

I have two Jtextarea 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 jtextarea and click send button it should be received back and printed in the second jtextarea.

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);
  }
}

Just flush the stream:

ps.println(area.getText());
ps.flush();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM