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