java.net.ConnectException: Connection timed out: connect
This exception caused by several issues. Issue may be solved by ensuring following checklist.
- Connected to Internet / Local Area Network (LAN)
Make sure you are connected to internet or local area network.
- Behind Firewall
Firewall is common issue faced by many developer. We were facing same issue.
java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) 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.http.HttpClient.(HttpClient.java:233) at sun.net.www.http.HttpClient.New(HttpClient.java:306) at sun.net.www.http.HttpClient.New(HttpClient.java:323) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172) at ReadWebpage.main(ReadWebpage.java:24)
Scenario
We were writing an applet program in Java to read webpage from internet. We can't even make request to well know search engine www.google.com even if its allowed in firewall rule. We called upon our network admin he said you must pass your request through proxy server. We set proxy in our Java code for HttpURLConnection and all went good.
Code
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.202", 8080)); HttpURLConnection con = (HttpURLConnection) data.openConnection(proxy);Use of Proxy
You need to take care for proxy connection and it depends upon whether you are write program for applet or desktop application. If you are writing code for applet then in production applet, you don't need proxy connection as server will have all access and will work without proxy. If you are working on desktop application that run within the Local Area Network then you must set proxy in case of firewall.
0 comments :