jQuery in Action, 2nd edition*
The moose likes Sockets and Internet Protocols and the fly likes Connection timed out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Connection timed out" Watch "Connection timed out" New topic

Connection timed out

varunvarun goyal

Joined: Feb 16, 2006
Posts: 2
hi all,
i made a program wich gets the urls in a particular web page ,i tested this prog on other terminals it works fine but with me gives the exception as:

java.net.ConnectException: Connection timed out: connect

can any suggest a reason or some settings required..
Joe Ess

Joined: Oct 29, 2001
Posts: 8876

"Connection timed out" usually means that the application was able to connect to the remote server but the response was not returned in a timely manner (If you couldn't connect at all you would have received a "Connection refused" message). Can you access the same URL from a browser on the various terminals? Is there something weird with this one terminal (old hardware, slow connection, old JVM)? Can you post a small sample of code that exhibits this behavior.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
varunvarun goyal

Joined: Feb 16, 2006
Posts: 2
///this is my class where i retrieve page... and urls present in it ..

package Connection;

* Created by IntelliJ IDEA.
* User: varungoyal
* Date: Feb 10, 2006
* Time: 10:52:28 AM
* To change this template use File | Settings | File Templates.

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;
import java.util.StringTokenizer;
public class SendPostRequest {
private static InputStream stream= null;

public String run(String ur )
String webdata=null;

//url object
URL myURL=new URL(ur);
//url connection object
HttpURLConnection urlc=(HttpURLConnection) myURL.openConnection();
System.out.print("______________control here___________after time out");

int a=urlc.getReadTimeout();
System.out.print("time out is"+a);

System.out.print("...Status of Connnection...."+urlc.getResponseCode()+"\n");

stream = urlc.getInputStream();
int size = stream.available();
System.out.print(" Size of available stream is...."+size+"\n" );
byte[] data=new byte[size];
int num_read = stream.read(data);
byte[] actual_data=new byte[num_read];
webdata=new String(actual_data);
if(stream.read()== -1)
return webdata ;
} //end of try
catch(Exception exc)

} //end of first try
catch(Exception ex)
System.out.println("connection time out");
return webdata;
} //end of method run

public Vector returnUrl(String page)
//System.out.println("__________ here _____________________"+page);
Vector vec = new Vector();
StringTokenizer st = new StringTokenizer(page,"\"");

while( st.hasMoreTokens()){
String str = (String)st.nextElement();
if( str.indexOf("href") != -1){
str = (String)st.nextElement();
str = str.replaceAll("&","&");

if(str.indexOf("&z=") != -1){
int zIndex = str.indexOf("&z=");
str = str.substring(0,zIndex)+str.substring(zIndex+9,str.length());

if( vec == null )
System.out.println(".........\" no URL found in the page\"..........");
return vec;

My exception is ::::::::::
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.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
at sun.net.www.http.HttpClient.New(HttpClient.java:287)
at sun.net.www.http.HttpClient.New(HttpClient.java:299)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:792)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:744)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:669)
at Connection.SendPostRequest.run(SendPostRequest.java:31)
at Connection.ProcessUrl.main(ProcessUrl.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
connection time out
problem in gettig response from URL :..."xyz"(any url which is feeded like yahoo or google...)

i think it wil work on ur pc wel becoz i tested it on other terminals as wel and it works fine ..and regarding my pc there is no old config problem or jvm problem..please check the code if any methods can be added .....
I agree. Here's the link: http://aspose.com/file-tools
subject: Connection timed out