Refusing connection while trying to connect localhost - java

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)

Related

RMI connection refused on localhost

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.

java.sql.SQLException: Network error IOException: No route to host?

14:51:34,652 DEBUG [mchange.v2.resourcepool.BasicResourcePool ] - An exception occurred while acquiring a poolable resource. Will retry.
java.sql.SQLException: Network error IOException: No route to host: connect
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Caused by: java.net.NoRouteToHostException: No route to host: 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 net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
Can anyone tell me:
How can resolve this issue ?
This is because you get back an ICMP "destination host unreachable" message (type 3, code 1).
This means either of those things:
you don't have a route to the destination host in your routing table (or a router you use doesn't have one);
a firewall sends this message back to you.
In the first case, fix the route if you can (maybe you have forgotten to initiate a VPN?). In the second case, ask the firewall admin why this occurs (luckily, he/she won't answer you "because yes" but give you a real reason :p)
This could occur if you are not connected to the server. Try pinging it to make sure.
check the iptables service is stopped.
"service iptables status"
And verify from your system by the following command,
"telnet server_ip 3306"
ref: https://community.oracle.com/thread/1335778?start=0

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.

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