Unhandled exception type UnknownHostException

Discussion in 'Java' started by Linus, Feb 25, 2005.

  1. Linus

    Linus Guest

    Someone can tell me why running the code it returns the error:
    Unhandled exception type UnknownHostException

    import java.io.*;
    import java.net.*;


    public class ArrayClient {


    public static void main(String argv[]) {

    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    //un pacchetto
    try
    {
    final InetAddress localAddr2 = InetAddress.getLocalHost();
    pacchetto pak=new pacchetto(1,"217.9.65.34",localAddr2,999);
    System.out.println("Questo รจ pacchetto "+ pak );
    InetAddress destinazione;




    // two arrays
    int dataset1[] = {3, 3, 3, 3, 3, 3, 3};
    int dataset2[] = {5, 5, 5, 5, 5, 5, 5};
    try {
    // open a socket connection
    Socket socket = new Socket("127.0.0.1", 4000);
    System.out.println("Contenuto di Socket: "+ socket );
    // open I/O streams for objects
    oos = new ObjectOutputStream(socket.getOutputStream());
    ois = new ObjectInputStream(socket.getInputStream());
    // create two serialized objects
    SerializedObject so1 = new SerializedObject();
    SerializedObject so2 = new SerializedObject();
    SerializedObject result = null;
    int outArray[] = new int[7];
    so1.setArray(dataset1);
    so2.setArray(dataset2);
    // write the objects to the server
    oos.writeObject(so1);
    oos.writeObject(so2);
    oos.flush();
    // read an object from the server
    result = (SerializedObject) ois.readObject();
    outArray = result.getArray();
    System.out.print("The new array is: ");
    // after unpacking the array, iterate through it
    for(int i=0;i<outArray.length;i++) {
    System.out.print(outArray + " ");
    }
    oos.close();
    ois.close();
    }
    catch (UnknownHostException e)
    {
    System.err.println ("Can't detect localhost : " + e);
    }

    } catch(Exception e) {
    System.out.println(e.getMessage());
    }
    }
    }

    //THE PACCHETTO CLASS:

    import java.io.Serializable;
    import java.net.*;

    public class pacchetto implements Serializable
    {
    int hopToLive=0;
    InetAddress destinatario;
    InetAddress mittente;
    InetAddress[] nodiIntermedi= new InetAddress[5];
    int time;

    pacchetto(int i,String j, InetAddress k, int l)
    {
    hopToLive=i;
    destinatario=InetAddress.getByName(j);
    mittente=k;
    for (int m=0; m<4;m++)
    {
    nodiIntermedi[m]=null;
    }
    time=l;

    }

    public int getNumeroHop()
    {
    return hopToLive;
    }

    public void setNumeroHop(int i)
    {
    hopToLive=i;
    }



    public InetAddress getDestinatario()
    {
    return destinatario;
    }

    public void setDestinatario(InetAddress i)
    {
    destinatario=i;
    }

    public InetAddress getMittente()
    {
    return mittente;
    }

    public void setMittente(InetAddress i)
    {
    mittente=i;
    }

    public InetAddress getNodiIntermedi(int i)
    {
    return nodiIntermedi;
    }

    public InetAddress[] getNodiIntermedi()
    {
    return nodiIntermedi;
    }

    public void setNodiIntermedi(InetAddress value,int i)
    {
    nodiIntermedi = value;
    }

    public int getTime()
    {
    return time;
    }

    public void setTime(int i)
    {
    time=i;
    }




    }
    Linus, Feb 25, 2005
    #1
    1. Advertising

  2. Linus

    Tilman Bohn Guest

    In message <>,
    Linus wrote on 25 Feb 2005 02:49:35 -0800:

    > Someone can tell me why running the code it returns the error:
    > Unhandled exception type UnknownHostException


    But that is not the real error you get. The real error message
    actually tells you how to fix it. Either catch UnknownHostException in
    your pacchetto class, or declare that you throw it. The compiler is even
    so nice as to tell you the exact line number where you call
    InetAddress.getByName() and prints that line out for you.

    As an aside, and referring to http://www.physci.org/codes/sscce.jsp ,
    your example is neither short, nor self-contained, nor correct. Your
    code does not compile as posted (apart from that particular error).
    There is no SerializedObject type anywhere. Your code is also not
    minimal, since all the stuff you're doing with those is not necessary to
    reproduce the problem in the first place.

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 25, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lee Simpson
    Replies:
    0
    Views:
    1,557
    Lee Simpson
    Jul 18, 2003
  2. Ola
    Replies:
    0
    Views:
    522
  3. Tedka
    Replies:
    2
    Views:
    2,656
    Mr. Dot Net
    Jul 19, 2004
  4. NicK chlam via DotNetMonster.com
    Replies:
    0
    Views:
    4,844
    NicK chlam via DotNetMonster.com
    Mar 21, 2005
  5. Warren Tang
    Replies:
    1
    Views:
    864
    Warren Tang
    Sep 23, 2008
Loading...

Share This Page