RMI connection refused on localhost - java

I am trying to learn RMI coding and when I run server side of RMI I get connection refused.
This is my server main method
public static void main(String[] args)throws Exception {
Implementation impl=new Implementation();
Naming.rebind("//localhost:2020/RMI", impl);
System.out.println("Implementation has been bind to the name RMI and is ready for use");
}
I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code. The exception I am getting is this
Exception in thread "main" 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.rebind(Unknown Source)
at java.rmi.Naming.rebind(Unknown Source)
at epl362Project.Server.main(Server.java:10)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.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)
... 7 more
What am I doing wrong? both client and server sides will run on my laptop as this is just an exercise. Am I missing something or is there something wrong with my code? Please besides giving me the answer explain why it is so since I do not just want to make it work, I am trying to learn.
**EDIT
I found out why my code was not working. I was missing a line of code that said
LocateRegistry.createRegistry(2020);
which I found from other questions here on stackoverflow. But there was no explanation nor did I find one online as to why this code was needed for RMI to work properly. Does anyone have an explanation?

The target of Naming.rebind() is the RMI Registry. It has to be running. It wasn't running. So you couldn't connect to it. So you got a ConnectException.
I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code.
This is both meaningless and irrelevant. Interfaces do not 'run the code'. Methods in classes run the code, and in this case the implementation class runs the code. However your problem is not in 'run[ning] the code', it is in connecting to the Registry.

Related

Failed to connect to MySQL database

I get this console error every time I try to run this rs private server. Pretty much everything is connected to a mysql db.
I've been messing around with this for quite awhile but can't figure out whats wrong. If there is any specific files you may need to look at to help me figure out whats wrong, tell me and I'll post it.
All help would be highly appreciated!
[Stage - 1] Launching Elunity...
[Stage - 2] Loaded object configurations
[Stage - 3] Loaded region configurations
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 1 432 637 479 787 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
Failed to connect to MySQL database
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2214)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:773)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at paladious.model.players.Highscores.process(Highscores.java:17)
at paladious.Server.main(Server.java:117)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 1 432 637 479 777 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137)
... 13 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.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 com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:290)
... 14 more
[Stage - 4] Loaded single event manager
[Stage - 7] Loaded NPC drops
[Stage - 8] Loaded player punishments and starters
[Stage - 10] Elunity online on port :43594
Thanks in advance! :)
This is a wrapped exception and not really interesting. It is the root cause of the exception which actually tells us something about the root cause. Please look a bit further in the stacktrace. The chance is big that you'll then face a SQLException: Connection refused or SQLException: Connection timed out.
If this is true in your case as well, then all the possible causes are:
IP address or hostname in JDBC URL is wrong.
Hostname in JDBC URL is not recognized by local DNS server.
Port number is missing or wrong in JDBC URL.
DB server is down.
DB server doesn't accept TCP/IP connections.
Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
To solve the one or the either, follow the following advices:
Verify and test them with ping.
Refresh DNS or use IP address in JDBC URL instead.
Verify it based on my.cnf of MySQL DB.
Start it.
Verify if mysqld is started without the --skip-networking option.
Disable firewall and/or configure firewall/proxy to allow/forward
the port.

MailConnectException in Java

I am try to take unread mails from gmail. There are lots of example code and I used them. The problem is, code doesn't work in every area. For example when I was at home the code works but in working area it is not. I think it is about network settings or different ports etc...
I am using the code that is already implemented in this a link. (First answer)
When I run that script at home it works. In my working are, it doesn't and the error message's is at below.
The question is how can i learn the which port should I use ? or Should I do another thing that I miss ?
Any directions, links or codes are appreciated.
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at gmailYeni.SendMail.sendFromGMail(SendMail.java:52)
at gmailYeni.SendMail.main(SendMail.java:20)
`Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 5 more
`
It seems you are sending mail from gmail. Then please check if your firewall allows access to gmail. If yes, try changing port# from 25 to 465 and try. But it seems an issue that your firewall is blocking access to gmail.

Refusing connection while trying to connect localhost

I was trying to develop a p2p file transfer application on java and as for the beginning I decided to run some transfer tests using localhost, as for the server, between local drives by some codes I found on the internet marked as working. The problem is for every port number I tried so far(+20) I got a "connection refused" error. I've installed microsoft loopback adapter as precaution, yet couldn't find any way to solve it. Any help would be appreciated.
just in case, I'm writing some code part related to socket in the client class.
// localhost for testing
Socket sock = new Socket("127.0.0.1",15123);
System.out.println("Connecting...");
and here is the error message
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.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 Client.main(Client.java:12)
line 12 is Socket sock = servsock.accept();
Most often , I saw that this exception comes when there is no service available to listen.Try to use another(free) port and make it sure that your server is running.You can find free port by writing netstat -a -o -n in cmd(on windows)

Java sockets - java.net.ConnectException: Connection refused: connect

I've created a simple chat program which communicates using sockets. Everything works fine when I'm running it on localhost. However, the problems occur when I try to link the client and server programs using my IP.
http://www.canyouseeme.org/ can connect to my server on port 9999 so I know that the server is fine and the port is open. However, my client can't connect.
The error log...
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.SocksSocketImpl.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 Client.connect(Client.java:129)
at Client.main(Client.java:47)
Does anybody have any idea what might be causing this? Thanks in advance.
Edit:
Links to the full source code:
http://pastebin.com/2XftHtn9
Have a look at the answers to: java.net.ConnectException: Connection refused
My first suspicion however would be a firewall issue.....
Is your client on same LAN as your server? I think you should re-check IP address / host name and port number to which your client is connecting.

mysql jdbc communicationexception

I have BLManager class that first open connection and called Data manager class to get data from database and return list of VO containing that data.
After completing all calculation in BL manager i call the method that commit transaction and close the connection.
This is the flow that i follow for opening and closing database connection. Today i face some strange exception described as follow
com.mysql.jdbc.CommunicationsException: The driver was unable to create a connection due to an inability to establish the client portion of a socket.
This is usually caused by a limit on the number of sockets imposed by the operating system. This limit is usually configurable.
For Unix-based platforms, see the manual page for the 'ulimit' command. Kernel or system reconfiguration may also be required.
For Windows-based platforms, see Microsoft Knowledge Base Article 196271 (Q196271).
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2104)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.cc.jas.datamanager.util.db.DBManager.getConnection(DBManager.java:83)
at com.cc.jas.blmanager.base.BaseBLManager.startTransaction(BaseBLManager.java:252)
at com.cc.jas.blmanager.system.systemconfig.SystemConfigBLManager.getConfigMap(SystemConfigBLManager.java:82)
at com.cc.jas.delegate.system.systemconfig.SystemConfigBusinessDelegate.getConfigMap(SystemConfigBusinessDelegate.java:39)
at com.cc.jas.common.cache.SystemConfigCacheManager.reloadCache(SystemConfigCacheManager.java:120)
at com.cc.jas.common.cache.SystemConfigCacheManager.getParameterValue(SystemConfigCacheManager.java:53)
at com.cc.jas.model.base.BaseReportVO.getDisplayDate(BaseReportVO.java:116)
at com.cc.jas.model.account.rvo.AccountItemReportVO.getDateString(AccountItemReportVO.java:111)
at sun.reflect.GeneratedMethodAccessor333.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1132)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:115)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:100)
at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:104)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:787)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:751)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1422)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:879)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:801)
at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:536)
at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:63)
at net.sf.jasperreports.engine.fill.JRThreadSubreportRunner.run(JRThreadSubreportRunner.java:209)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.BindException: Address already in use: 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.SocksSocketImpl.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 com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
What this exception says and what is solution for that ?
Caused by: java.net.BindException: Address already in use: connect
This says that the ip:port you are trying to bind with process is already binded with some other process.
try doing ping 127.0.0.1 to see if tcp/ip is working or not.then use netstat -a to see the open ports and sockets.

Categories