Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过RMI发送存储在ArrayList中的数据_Java_Swing_Arraylist_Client Server_Rmi - Fatal编程技术网

Java 通过RMI发送存储在ArrayList中的数据

Java 通过RMI发送存储在ArrayList中的数据,java,swing,arraylist,client-server,rmi,Java,Swing,Arraylist,Client Server,Rmi,如果我希望客户机请求存储在服务器的ArrayList中的文本数据,那么使用JavaRMI的实现过程将如何 请提供类图或示例代码。为服务器和客户端定义一个接口(相同的包名) 添加IArrayList的实现(在服务器端) 服务器 然后运行客户端。你得到这个输出 非常感谢,但是您能解释一下Constants.java实用类的用途吗?它只是为了更好地组织您的工作。并通过RMI\u ID使远程对象的RMI连接唯一。同样,如果答案帮助你,考虑接受答案和投票。谢谢,对你有用吗?在我的IDE中,它工作得非常完美

如果我希望客户机请求存储在服务器的ArrayList中的文本数据,那么使用JavaRMI的实现过程将如何

请提供类图或示例代码。

为服务器和客户端定义一个接口(相同的包名)

添加
IArrayList
的实现(在服务器端)

服务器

然后运行客户端。你得到这个输出


非常感谢,但是您能解释一下Constants.java实用类的用途吗?它只是为了更好地组织您的工作。并通过
RMI\u ID
使远程对象的RMI连接唯一。同样,如果答案帮助你,考虑接受答案和投票。谢谢,对你有用吗?在我的IDE中,它工作得非常完美@AlaaAbuZarifayes它是有效的,但我学习它的方式我没有用你的方式绑定客户端和服务器。。我在sever类| |=>IArrayList c=new ArrayListImpl()中这样使用它;命名。重新绑定(“rmi://localhost:/MyService“,c)`两种方法都是正确的。你可以用你想要的任何方式。这取决于您在它们之间进行选择;)换句话说,如何实现RMI应用程序?太宽了。查找教程。
package arrayListRMI;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;

public interface IArrayList extends Remote {
    public ArrayList<String> getText()  throws RemoteException;
    public void setText(ArrayList<String> text) throws RemoteException;
}
package arrayListRMI;

public class Constants {    
    public static final String RMI_ID = "StackOverflowAnswer";
    public static final int RMI_PORT = 222 ;   
}
package arrayListRMI;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;


public class ArrayListImpl  extends UnicastRemoteObject implements IArrayList{

    private static final long serialVersionUID = 1L;
    private ArrayList<String> text;

    protected ArrayListImpl() throws RemoteException {
        super();
    }

    public ArrayList<String> getText() {
        return text;
    }

    public void setText(ArrayList<String> text) {
        this.text = text;
    }

}
ArrayList<String> textRequested = new ArrayList<String>();
textRequested.add("example1");
textRequested.add("example2");
ArrayListImpl arrayListToSend = new ArrayListImpl();
arrayListToSend.setText(textRequested);
package arrayListRMI;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.SQLException;


public class Server {
    public static void main(String args[]) throws AlreadyBoundException, SQLException, ClassNotFoundException {

        try {
            ArrayList<String> textRequested = new ArrayList<String>();
            textRequested.add("example1");
            textRequested.add("example2");
            ArrayListImpl arrayListToSend = new ArrayListImpl();
            arrayListToSend.setText(textRequested);
            Registry registry = LocateRegistry.createRegistry(Constants.RMI_PORT);
            registry.bind(Constants.RMI_ID, arrayListToSend);
            System.out.println("Server starts....");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
package arrayListRMI;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

public class Client {
    public static void main(String[] args) {
        try {
            Registry registry = LocateRegistry.getRegistry("localhost", Constants.RMI_PORT);
            IArrayList cmp = (IArrayList) registry.lookup(Constants.RMI_ID);
            ArrayList<String> received = cmp.getText();
            System.out.println(received);
        } catch (Exception e) {
            System.err.println("Client exception: " + e.toString());
            e.printStackTrace();
        }
    }
}
Server starts....
[example1, example2]