Wednesday, January 6, 2010

Part 2: NoRouteToHostException

Part1 : Part2

If following code run without proxy details, then java.net.NoRouteToHostException will be thrown in windows/linux platform


import java.net.Socket;
public class SocketPrg {
public static void main(String[] args) throws Exception{
Socket sock=new Socket("abc.javafundu.com",8001);
}
}


UnknownHostException

Assume, I have given proper proxy details if internet connection established through proxy server. If we refer wrong host name which does not exsit then we do get java.net.UnknownHostException.

Additionally, we may refer right host name then also we do get this exception. In this case we have to check hosts file and correct the entry.

windows: /windows/system32/drivers/hosts
Linux : /etc/hosts

Make following entry in hosts file
127.87.6.207 abc.javafunud.com javafundu.com
Exception will be thrown as below


Exception in thread "main" java.net.UnknownHostException: abc.javafundu.com
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 SocketPrg.main(SocketPrg.java:7)


ConnectException

If host name is correct and hosts points right IP address with wrong port number then ConnectException will be thrown with Connection timed out for remote host most of the time( with default timeout configuration). For localhost or same network host, we do get Connection refused.

Socket sock=new Socket("localhost",8001);


Exception in thread "main" 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 SocketPrg.main(SocketPrg.java:7)

Socket sock=new Socket("javafundu.com",8001);


Exception in thread "main" 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.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at SocketPrg.main(SocketPrg.java:9)

Reactions:

0 comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More