简体   繁体   中英

Getting “ObjectID already in use” error while while connecting multiple clients to RMI server using Java

In our project, we're trying to connect multiple clients to a single server. Client is calling all the functions defined at RMI Server on Button-CLick using RMI function call. Server is also multi-threaded. On 1st Client connection, all the RMI Servers start normally on their respective ports but when 2nd Client tries to connect, a new thread for client is created but RMI servers aren't started as the ports are already in use. Here it gives "ObjID Already" in use error.

We've to provide functionality to client for File Uploading & Downloading using different button clicks on different Swing Client forms which eventually calls RMI functions at Server side.

Please suggest what should we do??

Is there any better way to do multiple-client server side function calls?

Server Side Begin.java Code

package sedas;
public class Begin implements Runnable {
protected static Socket clientsocket;
public static String directorypath = "C:\\BEProject\\";


public Begin(Socket sock) {
    Begin.clientsocket = sock;
}

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(15127);
    System.out.println("Listening");
    while (true) {
        Socket sock = ssock.accept();
        System.out.println("Connected to " + sock.getInetAddress());
        new Thread(new Begin(sock)).start();
    }
}

@Override
public void run() {
    try {
        RMILogIn rmilogin = new RMILogIn();

//Port for RMILogIn is 3230 defined in RMILogIn.java file
//Similarly for all others RMI files
        RMIRegistration rmiregister = new RMIRegistration();
        RMILogout rmilgout = new RMILogout();
        RMISendFiles rmiSend = new RMISendFiles();
        RMIShowFiles rmiSendfiles = new RMIShowFiles();
        RMIGetFiles newrmi = new RMIGetFiles();

    } catch (RemoteException ex) {
        Logger.getLogger(Begin.class.getName()).log(Level.SEVERE, null, ex);
    }

}
}

Client Side Connectivity Code

package sedas;

public class Sedaslogin extends javax.swing.JFrame {

OutputStream osuname = null;
private static Component frame;
public static InetAddress address;
public static Socket socket;

/**
 * Creates new form
 */
public Sedaslogin() {
    initComponents();
}                       
private void initComponents() {

    jDesktopPane1 = new javax.swing.JDesktopPane();
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    username = new javax.swing.JTextField();
    password = new javax.swing.JPasswordField();
    login = new javax.swing.JButton();
    signup = new javax.swing.JButton();
    jLabel3 = new javax.swing.JLabel();


private void signupActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    this.setVisible(false);
    new Registration().setVisible(true);
}                                      

private void loginActionPerformed(java.awt.event.ActionEvent evt) {                                      
     try {

    LoginInterface RMILogin;
    Registry registry;
    String serverAddress = address.getHostAddress();
    String serverPort = "3230";
    //System.out.println("sending " + text + " to " + serverAddress + ":" + serverPort);
    try {

        registry = LocateRegistry.getRegistry(serverAddress, (new Integer(serverPort)));
        // look up the remote object
        RMILogin = (LoginInterface) (registry.lookup("RMILogIn"));
        // call the remote method
        osuname = socket.getOutputStream();
        DataOutputStream doucred = new DataOutputStream(osuname);
        doucred.writeUTF(username.getText());
        char[] pw = password.getPassword();
        String password = new String(pw);
        doucred.writeUTF(password);
        RMILogin.LogIn();


        //System.out.print(password);
        InputStream is = socket.getInputStream();
        DataInputStream status = new DataInputStream(is);
        String receivedstat = status.readUTF();
        if (receivedstat.equals("Successful")) {
            this.setVisible(false);
            new Choice().setVisible(true);
        } else if (receivedstat.equalsIgnoreCase("error")) {
            jLabel3.setText("Invalid Username/Password");
        }
    } catch (RemoteException | NotBoundException e) {
    }
   } catch (IOException ex) {
        Logger.getLogger(Sedaslogin.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public static void main(String args[]) {
    try {
        // TODO add your handling code here:
        address = InetAddress.getByName("hp-hp");
        //System.out.println(address.getHostAddress());
        socket = new Socket(address.getHostAddress(), 15127);
        JOptionPane.showMessageDialog(frame, "Connection Accepted by Server!");
    } catch (IOException ex) {
        Logger.getLogger(ClientUpload.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(frame, "Connection Rejected by Server!");
        System.exit(1);
    }

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Sedaslogin().setVisible(true);
        }
    });
}

PS: Assume all imports are done and Client has Swing GUI.

You're trying to create multiple Registries in the same JVM. You can't, unless you use a different port for each, and there's no point to that, and you don't need it in the first place.

You're doing this wrong from start to finish.

  • Get rid of the listening socket, the accept, and the thread.
  • Create and register one set of remote objects.
  • Redefine your remote interfaces to take parameters instead of writing them to the socket.
  • Have the client lookup the remote objects and just call them with parameters, getting rid of the clients sockets as well.

RMI is rather more powerful than you seem to think.

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