This question already has answers here:
Java RMI : connection refused
(2 answers)
Closed 5 years ago.
Why I continue to receive following error? I tried to fix a problem by following posts on similar issue, however nothing seems to work.
java.rmi.ConnectException: Connection refused to host: 10.0.0.57; nested exception is:
java.net.ConnectException: Operation timed out
at .....
javax.rmi.ssl.SslRMIClientSocketFactory.createSocket(SslRMIClientSocketFactory.java:120)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 5 more
public static void main(String[] args) throws UnknownHostException {
// Bind to registry
System.setProperty(
"javax.net.ssl.keyStore",
"/Users/xxxx/keys/printserver.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
System.setProperty(
"javax.net.ssl.trustStore",
"//Users/xxxx/keys/printserver.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "password");
System.setProperty("javax.net.ssl.debug", "all");
//System.out.println(java.net.InetAddress.getLocalHost());
try {
System.setProperty("java.rmi.server.hostname", "191.234.6.152");
Registry r = LocateRegistry.getRegistry(null, 5099, new SslRMIClientSocketFactory());
r.bind("LoginServer", new LoginServer());
System.out.println("--- Login Server ready ---");
}
No I have not started the Registry.
Stop right there. You're not going to get anywhere until you start the Registry. You can't connect to something that isn't there.
It's strange that you're getting a connection timeout rather than 'connection refused', but there is nothing else here that needs explaining.
Setting java.rmi.server.hostname won't fix this problem. None of the questions you have cited here is a similar issue. They are all connection refusals in the client when executing a remote method. This is a connection refusal in the server when binding to the Registry.
Related
I have a question about MySQL/JDBC connections in Java.
I wrote an application that successfully communicates with a database, but the issue that I recently found out was that my DB connection was dropping, and I need the application to have a connection to the DB at all times.
This is a small snipplet of the error I was getting:
com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 89,225,584 milliseconds ago. The last packet sent successfully to the server was 89,225,584 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003)
This is also a snipplet of the constructor for my DBConnections class:
private final String url = "jdbc:mysql://localhost:3306/", database.....;
private Connection connection;
public DBConnector(){
try {
// Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url+database, username, password);
} catch (Exception ex) {
System.out.println("Error: " + ex);
}
}
In the errors section, I noticed it's telling me to add autoReconnect=true, I wondered; will my connection still stay up for longer if I structured the connection class like this:
connection = DriverManager.getConnection(url+database+"?autoReconnect=true", username, password);
If not, what else could I do to make sure my connection doesn't drop?
What I would suggest is to use a connection pool (Apache DBCP or HikariCP - the last one is currently having the best performance out of all solutions on the market) with configuration of testing connection before borrowing it from the pool. Depending on the library there should be an option like setTestOnBorrow(true).
In real applications you should always use connection pool instead of manually handling connections.
I'm working on an Android app (Java 8, apache.commons.net.ftp 3.6, Min: API 24/target: API 27) that connects to an FTP server but I'm experiencing a weird problem:
If I connect to one of the public test servers (e.g. speedtest.tele2.net) or the IP address directly (e.g. 10.1.1.123), it works fine.
If I add a typo in a "normal" address on purpose (e.g. sspeedtest.tele2.net), I get the expected java.net.UnknownHostException but if I add a typo to an IP address, no matter if it's in the same network or not (e.g. 10.1.1.223), nothing else happens - no exception, no error, no result, even after the set timeout time has passed.
Code:
FTPClient f = new FTPClient();
f.setDefaultTimeout(5000); //5 seconds
try {
f.connect(url,port);
boolean b = f.login(username,password);
Log.d(TAG,"logged in="+b+", connected="+f.isConnected());
} catch (IOException e) {
e.printStackTrace();
}
I let it run a little bit longer and finally, after 1 minute and 16 seconds an exception was thrown:
W/System.err: java.net.ConnectException: Connection refused
I've tried this multiple times and the exception always seems to be thrown after 1:16.
How do I make this respect the default timeout too? Is there a different one I have to use?
You're setting a timeout for receiving data. The timeout for opening a connection is separate, and set with the setConnectTimeout(int connectTimeout) method:
FTPClient f = new FTPClient();
f.setConnectTimeout(5000); // 5 second timeout to open connection
f.setDefaultTimeout(5000); // 5 second timeout when receiving data
If you get "connection refused" that means the IP address is actually used by some host in the network. If there was nothing at that address, you would get an error more along the lines of "no route to host".
I'm having issues connecting to a Database server with the JDBC driver in Netbeans. I've tried everything, enabling TCP/IP, opening the ports, I've followed tutorials online. It just won't work.
This is the error message I get in the console:
Information: Error: The TCP / IP connection could not be made to the MANUEL-PC host, port 1433. Error: "Connection refused: connect Verify the connection properties, check that there is an instance of SQL Server running on the host and accepting TCP / IP connections on the port and verify that there is no firewall blocking TCP connections on the port. "
Start of the class we're using...
public class DBPosteo
{
private final String URL ="jdbc:sqlserver://MANUEL-PC\\SQLEXPRESS:1433;databaseName=DLC_MotorDeBusqueda;integratedSecurity=true";
private Connection con;
String query = "";
PreparedStatement pstmt;
ResultSet rs;
public void init()
throws ClassNotFoundException, SQLException
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(URL);
}
DB Server name in SQL Management Studio
Thanks in advance for the help... I've never struggled so much with Databases in my life :)
Remove the port number.
Only specify either instance name (SQLEXPRESS) or port number (1433), never both.
Since port 1433 is reserved for the unnamed instance, the SQLEXPRESS named instance would be on a different port, and unless you specifically configured it (unlikely), that port is dynamic and can change on reboots, so you need the named lookup.
I am new and I've a question.
In android, I can't connect any remote address via TCP Socket. When I tried to connect, debugger shows an error:
Exception: failed to connect to /23.20.47.114 (port 9339) after 2000ms: connect failed: EINVAL (Invalid argument), OSVersion: 4.1.1(Jellybean)
And the code:
void run(){
Socket s=new Socket();
s.bind(getAddress("192.168.0.45",8080)); <-It's bound successfully.
s.connect(getAddress("23.20.47.114",9339)); <-Error
writeData(s);
}
InetSocketAddress getAddress(String host, int port){[code]}
void writeData(Socket so){[code]}
Note: This server is always open and sorry for my english.
You already bound the socket to a local IP address using bind(), so it does not make sense to also connect the same socket to a remote server. Try getting rid of the bind() statement.
I've seen few threads about this topic but I still can't figure out what's wrong. Following is the code:
import java.sql.*;
public class SQL
{
public static void main(String[] args)
{
Connection conn = null;
String url = "jdbc:mysql://mysql1.oyo.co.il:3306/";
String dbName = "sdarot2_winner";
String driver = "com.mysql.jdbc.Driver";
String userName = "";
String password = "";
String table = "LEADER_CAM_PRODUCTS";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}catch (Exception e) {
System.out.println(e);
}
}
}
And here is the error I get:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying > > exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection timed out: connect
STACKTRACE:
java.net.ConnectException: Connection timed out: 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.(Unknown Source)
at java.net.Socket.(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2744)
at com.mysql.jdbc.Connection.(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at SQL.main(SQL.java:17)
** END NESTED EXCEPTION **
Last packet sent to the server was 1 ms ago.
Here is few things I found around:
The I.P/domain or port is incorrect
The I.P/domain or port (i.e service) is down
The I.P/domain is taking longer than your default timeout to respond
You have a firewall that is blocking requests or responses on whatever port you are using
You have a firewall that is blocking requests to that particular host
Your internet access is down
a) it is correct.
b) it's not down since I got a website that is working right now with that database.
c) how can I check that?I don't think that it's the problem since as I said I got a running website on this database.
d) now way because the same reason I mentioned above.
e) samem as d
So what I'm doing wrong?how come a php code can work with sql but java gives me errors?
Thank you.
As an experiment, try connecting to port 3306 on mysql1.oyo.co.il using your system's telnet command. Or some other utility (for example nc or netcat) that opens a raw TCP/IP connection. Depending on your OS / distro, you may have to locate and install a suitable command to help you with this trouble-shooting. Observe what happens.
Re your diagnosis:
a) it is correct.
If you say so ...
b) it's not down since I got a website that is working right now with that database.
At best you have demonstrated that the host is working. The web service on port 80 and the MySQL service on port 3306 are different services. (And in fact, it is possible that mysql1.oyo.co.il is doing clever packet routing tricks so that traffic on different ports is being tranparently routed to different hosts or virtual hosts.)
c) how can I check that?
Try changing / increasing the timeout.
I don't think that it's the problem since as I said I got a running website on this database.
My guess it is not a timeout issue ... but as I said above, the fact you got a website says NOTHING about whether the MySQL service is running.
d) now way because the same reason I mentioned above.
You've only demonstrated that you can get to port 80 ... see above
e) samem as d
You've only demonstrated that you can get to port 80 ... see above
Based on the above, it is plausible that:
you have a local firewall problem,
the MySQL service on that port is not currently running,
networking routing on the service side are broken for traffic on that port,
the MySQL service has been permanently turned off, or
the service's firewall has been configured to not accept network connections from random (or specific) places, including yours.
The fact that you are seeing a timeout rather than a "connection refused" suggests that this is more likely to be a networking or firewalling issue than a problem with the MySQL service endpoint itself.
My guess is that "they" have withdrawn the service ... and that you are not supposed to use it anymore. (Providing a public MySQL database service strikes me as an odd thing to do ... as well as being problematic from a security and administration standpoint.)
'Connection timed out' indicates either:
a server-side firewall that is deliberately ignoring your connection request
a network topology problem such that IP packets aren't getting through
the server host itself is down.
Most likely it is the first.
Note that, contrary to other answers here, it doesn't indicate that the server program is down, or a local firewall problem at the client. The former would cause 'connection refused' and the latter would give you something along the lines of 'permission denied', depending on your platform.