This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Why my RMI connection throw exceptions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Why my RMI connection throw exceptions?" Watch "Why my RMI connection throw exceptions?" New topic
Author

Why my RMI connection throw exceptions?

long lingyu
Ranch Hand

Joined: Feb 15, 2004
Posts: 51
It throws exception:
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at suncertify.db.DataFactory.getDatabase(DataFactory.java:83)
at suncertify.client.AloneFileChooser$1.actionPerformed(AloneFileChooser.java:70)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 31 more
When I write a test program from rmi ,it run correctly.
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
Hi Long,

It might be helpful to post your DataFactory.getDatabase method code.


Regards, George
SCJP, SCJD, SCWCD, SCBCD
alzamabar
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Originally posted by long lingyu:
It throws exception:
at java.rmi.Naming.lookup(Unknown Source)
at suncertify.db.DataFactory.getDatabase(DataFactory.java:83)

Did you register the server with the Naming service?
Marco


Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
long lingyu
Ranch Hand

Joined: Feb 15, 2004
Posts: 51
My code:

[Andrew: put the source code between [code] and [/code] UBB tags]
[ March 11, 2004: Message edited by: Andrew Monkhouse ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11490
    
  95

Hi Long,
I'll repeat Marco's question: Did you register the server with the Naming service?
(Actually, I would go even further back: have you started the registry? )
You may want to put a temporary line into your code which will allow you to see what your lookupString contains. So change your code to:

You might want to double check that your registry is listening on the port number you are using. You can normally do this by running "netstat -an" from a command prompt:

See that line in bold? That tells me that I do have something running and listening on port 1099 (the standard RMI port).
This will look slightly different under MS Windows:

But the important bit is that it is listening on port 1099, and that it is not bound to a specific address (I don't think you can bind Sun's RMI Registry to a specific address, but it doesnt hurt to check).
Also, regarding your catch blocks:

You might might want to do something a little more user friendly for your final submission.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
long lingyu
Ranch Hand

Joined: Feb 15, 2004
Posts: 51
My server code:

What's the matter?
[Andrew: put the source code between [code] and [/code] UBB tags]
[ March 11, 2004: Message edited by: Andrew Monkhouse ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11490
    
  95

Hi Long,
I have edited your post to put the code between [code] and [/code] UBB tags. Doing this ensures that indenting is preserved, which makes the code easier to read.
When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.
If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.
I cannot see what is wrong with this code.
Did you confirm that
  • Your server was running in another window while you were trying to run the client (and had not displayed any errors)?
  • You could see that your server was listening by using the netstat -an command?
  • The output when you printed the contents of the lookupString does contain all the right fields?


  • Regards, Andrew
    long lingyu
    Ranch Hand

    Joined: Feb 15, 2004
    Posts: 51
    1.How can I keep the server active? in command line: start rmiregistry or in program? I don't know the method to keep the server active in program.
    2.Should there need a security file for the client connection?
    I have test a simple RMI program. It work good. The steps are:
    a. start rmiregistry <port>
    b. rmic -v1.2
    c. run server
    d. run client with arguement -Djava.security.policy
    It run.
    In the project,I can't connect the server. When I use createRegistry(port),there are no port in the netstat -an display. Why?
    George Marinkovich
    Ranch Hand

    Joined: Apr 15, 2003
    Posts: 619
    Hi Long,
    Originally posted by long lingyu:
    1.How can I keep the server active? in command line: start rmiregistry or in program? I don't know the method to keep the server active in program.

    Take a look at this thread: Topic: RMI Binding on localhost


    2.Should there need a security file for the client connection?

    No, it shouldn't be necessary.

    I have test a simple RMI program. It work good. The steps are:
    a. start rmiregistry <port>
    b. rmic -v1.2
    c. run server
    d. run client with arguement -Djava.security.policy
    It run.
    In the project,I can't connect the server. When I use createRegistry(port),there are no port in the netstat -an display. Why?

    Take a look at this thread: Topic: NX: Keep server running
    long lingyu
    Ranch Hand

    Joined: Feb 15, 2004
    Posts: 51
    I have solved this problem. I made a mistake. I start the server and then exit the JVM,the server is down. So the client can't connect to the server.
    long lingyu
    Ranch Hand

    Joined: Feb 15, 2004
    Posts: 51
    Another question,Should I package the policy file? or RMI don't need this file?
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11490
        
      95

    Hi Long,
    Glad to see your programs are now working.
    You should not need a policy file for this assignment, and the newer assignments (Hotel and Contractors) forbid "the installation of a security manager". So I recommend you do not use a policy file.
    (Note that although you should not need a policy file for this assignment, you may need one in real life if you ever need to use RMI).
    Regards, Andrew
     
    jQuery in Action, 2nd edition
     
    subject: Why my RMI connection throw exceptions?