Unhandled exception type UnknownHostException

L

Linus

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;
}




}
 
T

Tilman Bohn

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top