8 Replies Latest reply: May 8, 2012 7:29 AM by 935238 RSS

    Sending an object from client to server always on button press

    935238
      What I need is to send an object from client to server but I need to make server wait until another object is sent. What I have is the JFrame where you put the wanted name and surname, then you create a User object with these details and on button press you send this object to the server. I just can't hold the connection because when I send the first object, server doesn't wait for another button click and throws EOFexception. Creating the while loop isn't helpfull as well because it keeps sending the same object again and again. The code is here
      public class ClientFrame extends JFrame {
      
          private JButton btnSend;
          private JTextField txfName;
          private JTextField txfSurname;
      
          public ClientFrame() {
              this.setTitle(".. ");
      
              Container con = this.getContentPane();
              con.setLayout(new BorderLayout());
      
              txfName = new JTextField("name");
              txfSurname = new JTextField("surname");
      
              btnSend = new JButton(new AbstractAction() {
      
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      SSLSocketFactory f =
                              (SSLSocketFactory) SSLSocketFactory.getDefault();
                      try {
                          SSLSocket c =
                                  (SSLSocket) f.createSocket("localhost", 8888);
      
                          c.startHandshake();
      
                          OutputStream os = c.getOutputStream();
                          ObjectOutputStream oos = new ObjectOutputStream(os);
                          InputStream is = c.getInputStream();
                          ObjectInputStream ois = new ObjectInputStream(is);
      
                          
                          boolean done = false;
                          while (!done) {
                              String first = txfName.getText();
                              String last = txfSurname.getText();
                              User u = new User();
      
                              u.setFirstName(first);
                              u.setLastName(last);
                              oos.reset();
                              oos.writeObject(u);
      
                              String str = (String) ois.readObject();
                              if (str.equals("rcvdOK")) {
                                  System.out.println("received on the server side");
                              } else if (str.equals("ERROR")) {
                                  System.out.println("ERROR");
                              }
                          }
      
      
      
      
                          //oos.writeObject(confirmString);
      
                          oos.close();
                          os.close();
                          c.close();
      
                      } catch (ClassNotFoundException ex) {
                          Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);
                      } catch (IOException ex) {
                          System.err.println(ex.toString());
                      }
                  }
              });
              btnSend.setText("send object");
              con.add(btnSend, BorderLayout.PAGE_START);
              con.add(txfName, BorderLayout.CENTER);
              con.add(txfSurname, BorderLayout.PAGE_END);
              this.pack();
              setSize(200, 150);
              setVisible(true);
          }
      }
      
      public class TestServer {
      
          public static void main(String[] args) {
              
              try {
                  KeyStore ks = KeyStore.getInstance("JKS");
                  ks.load(new FileInputStream(ksName), ksPass);
                  KeyManagerFactory kmf =
                          KeyManagerFactory.getInstance("SunX509");
                  kmf.init(ks, ctPass);
                  SSLContext sc = SSLContext.getInstance("TLS");
                  sc.init(kmf.getKeyManagers(), null, null);
                  SSLServerSocketFactory ssf = sc.getServerSocketFactory();
                  SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(8888);
                  printServerSocketInfo(s);
                  SSLSocket c = (SSLSocket) s.accept();
       
      
                  InputStream is = c.getInputStream();
                  ObjectInputStream ois = new ObjectInputStream(is);
                  OutputStream os = c.getOutputStream();
                  ObjectOutputStream oos = new ObjectOutputStream(os);
                  boolean done = false;
                  User u;
                  
                    while(!done){
                        
                      
                      u = (User) ois.readObject();
                      String confirmString = "rcvdOK";
                      String errorString = "ERROR";
                      if (u != null) {
                          System.out.println(u.getFirstName() + " " + u.getLastName());
                          oos.writeObject(confirmString);
                      } else if (u == null) {
                          oos.writeObject(errorString);
                      }
                      
                      
                  
                  }
      
                  is.close();
                  s.close();
                  c.close();
      
              } catch (Exception e) {
                  
                      System.err.println(e.toString());
                  
                  
              }
          }
      
         
      }
      Thanks for any help, btw this doesnt need to be via ssl, the problem would be the same using only http. Please anyone help me:)

      Edited by: Vencicek on 7.5.2012 2:19

      Edited by: EJP on 7/05/2012 19:53

      Edited by: Vencicek on 7.5.2012 3:36