Add to Google Reader or Homepage

Exception in thread "main" UnknownHostException

UnknownHostException:

The UnknownHostException occurs when the java program uses the socket to create a connection to the remote host.There are two main reasons for this exception to occur.

1.This exception is usually thrown when DNS(Domain Name Server) is unable to translate the host name into a valid IP address.This could be a problem due to DNS server or the host name that you specified may be invalid.

2.The second reason is that "connection may be unavailable" for some reason.

Example:

 public class SocketTest
 {
    public static void main(String[] args)
    {
       try
       {
          Socket s = new Socket("net01.example.blospot.in", 2034);
          try
          {
             InputStream istream = s.getInputStream();
             Scanner in = new Scanner(istream);

             while (in.hasNextLine())
             {
                String line = in.nextLine();
                System.out.println(line);
            }
          }
          finally
          {
             s.close();
          }
       }
       catch (IOException e)
       {
          e.printStackTrace();
       }
    }
 }
 
 
In this above code the UnknownHostexception would occur when the socket tries to connect to
the remote host since the host name that i have been trying to connect is not available.
 
So if you come across this error ensure that whether you have given the valid  address or not.
If you indeed entered the valid address then check whether the connection is available. 

 

0 comments:

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top