Rmi服务器端口已经使用过

时间:2014-03-14 21:45:41

标签: java registry port rmi

我想在下面运行简单的rmi服务器代码。 首先,我写命令" rmiregistry 9260"所以我开始了rmi寄存器。 然后我运行以下代码,但我得到错误。 什么可能导致这些错误?请注意,我尝试了不同的端口号。

public class Server {

    public static void main(String args[]) {

        try {
            PaymentImpl robj = new PaymentImpl();
            Payment stub = (Payment) UnicastRemoteObject.exportObject(robj, 9260);

            Registry registry = LocateRegistry.getRegistry();
            registry.bind("Mortgage", stub);
            System.out.println("Mortgage Server is ready to listen... ");

        } catch (Exception e) {
            System.err.println("Server exception thrown: " + e.toString());
            e.printStackTrace();
        }
    }
}


Server exception thrown: java.rmi.server.ExportException: Port already in use
260; nested exception is:
        java.net.BindException: Address already in use
java.rmi.server.ExportException: Port already in use: 9260; nested exception

        java.net.BindException: Address already in use
        at sun.rmi.transport.tcp.TCPTransport.listen(Unknown Source)
        at sun.rmi.transport.tcp.TCPTransport.exportObject(Unknown Source)
        at sun.rmi.transport.tcp.TCPEndpoint.exportObject(Unknown Source)
        at sun.rmi.transport.LiveRef.exportObject(Unknown Source)
        at sun.rmi.server.UnicastServerRef.exportObject(Unknown Source)
        at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
        at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
        at Server.main(Server.java:14)
Caused by: java.net.BindException: Address already in use
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
        at java.net.ServerSocket.bind(Unknown Source)
        at java.net.ServerSocket.<init>(Unknown Source)
        at java.net.ServerSocket.<init>(Unknown Source)
        at sun.rmi.transport.proxy.RMIDirectSocketFactory.createServerSocket(
nown Source)
        at sun.rmi.transport.proxy.RMIMasterSocketFactory.createServerSocket(
nown Source)
        at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(Unknown Source)
        ... 8 more

1 个答案:

答案 0 :(得分:0)

如果您将9260用于单独的注册表进程,则无法在此过程中再次使用它。您可以通过使用LocateRegistry.createRegistry()在此过程中启动注册表来使用9260。将返回值存储到静态变量中以防止GC。