Connection timed out: connect Java [duplicate] - java

This question already has answers here:
What is a connection timeout during a http request
(2 answers)
Closed 27 days ago.
I get an exception when I run this code. Why?
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class MainClass {
public static void main(String[] args) throws Exception {
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
URL url = new URL("https://www.verisign.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}
Exception:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:204)
at java.net.URL.openStream(URL.java:1010)
at https.ssl.MainClass.main(MainClass.java:13)

We can't diagnose your networks for you. You need to do it yourself, or get your local admins to look at.
Things you should check before you bug your admins:
can you ping the host?
can you connect to http://www.verisign.com using a web browser?
can you connect to https://www.verisign.com using a web browser?
can you connect to http://www.verisign.com using your program?
can you connect to anything using your program?
The chances are that your problem is firewall related. My first guess would be that you don't have the correct environment variables or Java system properties set to tell the JVM to use a local proxy server for outgoing HTTP / HTTPS requests.
If it is not a problem with your settings, you will need to get help from someone local who can help you diagnose the problem.

Related

what are the possible reason for java.net.SocketTimeoutException

How can I check the possible reason for java.net.SocketTimeoutException.
I have a program that just pings to a website. however I am getting SocketTimeoutException. I know it is happening due to timeout expiring before the connection is established. But I want to know the precise reason of the issue. Like It is to do with internal DNS failure or due to unavailability of the internet or what !
Tried searching for similar questions but I didn't get the exact reason for not being able to establish the connection.
Below code snippet explains the problem
import java.net.HttpURLConnection;
import java.net.URL;
public class URLConnectionTest {
public static void main(String[] args) {
try {
String urlString = "https://www.google.com";
URL url = new URL (urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
con.connect();
System.out.println("Connected !!!");
}
catch(Exception e) {
System.out.println("Error occurred while connecting" + e);
}
}
}
Now there are multiple outputs for different case.
When I turned off laptop's wifi
Error occurred while connecting java.net.UnknownHostException: www.google.com
When I connect to wifi but router doesn't have the connectivity
Error occurred while connecting java.net.SocketTimeoutException: connect timed out
Few lines of Stacktrace for Timeout
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)

Connect to db2 server in windows

I am connecting to a db2 database server using this piece of java code.
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DatabaseConnect {
Connection co=null;
Statement st=null;
PreparedStatement pstmt;
ResultSet rs;
int status;
void makeConnection() throws ClassNotFoundException, SQLException
{
Class.forName("com.ibm.db2.jcc.DB2Driver");
co=DriverManager.getConnection("jdbc:db2://localhost:50000/databasename","username","password");
//st=co.createStatement();
}
Connection getConnection()
{
return co;
}
void endConnection() throws SQLException
{
co.close();
}
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
DatabaseConnect db=new DatabaseConnect();
db.makeConnection();
new MaintainHash().createHashMap(db.getConnection());
new Login(db.getConnection()).setVisible(true);
}
}
It works good if hostname is localhost but when i provide hostname of remote server, the program hangs and shows this error.
Exception in thread "main" com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.68.61] Exception java.net.ConnectException: Error opening socket to server /192.168.80.1 on port 50,000 with message: Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001
at com.ibm.db2.jcc.am.gd.a(gd.java:329)
at com.ibm.db2.jcc.am.gd.a(gd.java:410)
at com.ibm.db2.jcc.t4.ac.a(ac.java:439)
at com.ibm.db2.jcc.t4.ac.<init>(ac.java:96)
at com.ibm.db2.jcc.t4.a.b(a.java:358)
at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2066)
at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:780)
at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:725)
at com.ibm.db2.jcc.t4.b.<init>(b.java:333)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:199)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:474)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:115)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DatabaseConnect.makeConnection(DatabaseConnect.java:31)
at DatabaseConnect.main(DatabaseConnect.java:58)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.ibm.db2.jcc.t4.w.run(w.java:49)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.db2.jcc.t4.ac.a(ac.java:425)
... 14 more
Java Result: 1
BUILD SUCCESSFUL (total time: 25 seconds)
I am able to telnet and ping to the host ip address.
Got it.
The solution is actually quite simple as well as a bit off topic. Thanks Ian for pointing it out. I had to add port number of db2 to the inbound and outbound rules of firewall.
If anyone is having the same problem, you can follow these steps:
Go to Windows Firewall in control panel.
Click on Advanced Settings.
Select Inbound Rules and then click on New Rule.
Select Port option and then type the port number you want to add.
Repeat the above steps for outbound rules.

Getting connection time out while running Oozie workflow through Java

I am getting following error when i try to run Oozie workflow using java
IO_ERROR : java.net.ConnectException: Connection timed out: connect
at org.apache.oozie.client.OozieClient.validateWSVersion(OozieClient.java:234)
at org.apache.oozie.client.OozieClient.createURL(OozieClient.java:300)
at org.apache.oozie.client.OozieClient.access$000(OozieClient.java:71)
at org.apache.oozie.client.OozieClient$ClientCallable.call(OozieClient.java:366)
at org.apache.oozie.client.OozieClient.run(OozieClient.java:547)
at oozieDemo.main(oozieDemo.java:27)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
Here is my code:
OozieClient wc = new OozieClient("http:xxxxxxx/oozie");
System.out.println(" connection established....." + wc);
Properties conf = wc.createConfiguration();
conf.setProperty(OozieClient.APP_PATH,"hdfs:foo/xxx/workflow.xml");
conf.setProperty("jobTracker", "foo:8021");
conf.setProperty("nameNode","hdfs:xxxx");
conf.setProperty("queueName", "default");
conf.setProperty("appLibLoc","hdfs:/foo/xxx/lib");
String jobId = wc.run(conf);
System.out.println("Workflow job submitted");
So here I can see connection is getting established but unable to run the workflow.
I am new to this. So can't figure it out where exactly it is failing.
Connection timeout means either:
URL is incorrect or down, try pinging it.
Firewall is blocking it.
Default timeout expired.
Your internet access is down, which I'm going to assume isn't the case.

Query over Java Space connection?

Sample of the code:
ServiceTemplate tmp1 = new ServiceTemplate(null, classes, null);
try {
/* if(System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());*/
thisIp = InetAddress.getLocalHost();
LookupLocator locator = new LookupLocator("jini://"+thisIp.getHostName().toString());
ServiceRegistrar sr = locator.getRegistrar();
JavaSpace space = (JavaSpace)sr.lookup(tmp1);
System.out.println("Success");
}
Encountering error at [ ServiceRegistrar sr = locator.getRegistrar();]
which states
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:209)
at net.jini.core.discovery.LookupLocator.getRegistrar(LookupLocator.java:328)
at net.jini.core.discovery.LookupLocator.getRegistrar(LookupLocator.java:286)
at MessageEntry.main(MessageEntry.java:34)
I am new to JavaSpace tech, kindly assist me.
Thank You in Advance
Jeetesh.N
The ConnectException is a quite fundamental error which simply means that your Java process could not establish a socket connection to the target machine and port, because that machine wasn't accepting connections on the target port. In this case it's almost certainly trying to connect to your localhost IP address using the default JINI port.
I'd suggest that this error means you don't have a JINI service/registrar running on your machine at the time you executed the code.

Java HttpsURLConnection outbound limit?

I am creating a test app to load test a comet servlet by creating a bunch of outbound https connections (each looping in its own thread).
Is there a limit on the number of concurrent outbound http requests? If so, is it an OS level issue (xp 32bit) or Java? I get the following exception when running around 100 connections. The error does not occur instantly but after anywhere between 50-150 loops (sometimes more), leading me to suspect that it actually might be me not releasing resources correctly:
java.net.BindException: Address already in use: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:559)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at fqexconnectiontesting.FQEXHttpClient.run(FQEXHttpClient.java:158)
at java.lang.Thread.run(Thread.java:662)
My code:
while(true){
try{
connection =(HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("keep-alive", "true");
connection.setRequestMethod("GET");
input = new DataInputStream( connection.getInputStream() );
b = new byte[4096];
totalBytes += input.read(b);
input.close();
b = null;
connection.disconnect();
connection = null;
Thread.sleep(100);
}
Any ideas appreciated.
Thanks,
Dazz
By default Java opens only up to 5 persistent connections to the same host.
But your code uses the disconnect() method which AFAIK prevents persistent connections.
You should better read the documentation about Persistent Connections. Take the sample code near the end for getting a fully working persistent connection.
May be this fixes your problem.

Categories