Buon Giorno sto provando a fare una prova:
1) ottengo i dati da un socket e fin qui tutto bene il client lo riceve questo è il cliente che stampa il risultato correttamente:
codice:
public static void main(String[] args) {
try {
Socket socket = new Socket("192.168.1.67",6789);
ArrayList<String> arrayBusta = new ArrayList<String>();
try {
ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream()); //Error Line!
try {
Object object = objectInput.readObject();
arrayBusta = (ArrayList<String>) object;
for (int i = 0; i < arrayBusta.size(); i++) {
// String get = arrayBusta.get(i);
System.out.println(arrayBusta.get(i));
}
} catch (ClassNotFoundException e) {
System.out.println("The title list has not come from the server");
e.printStackTrace();
}
} catch (IOException e) {
System.out.println("The socket for reading the object has problem");
e.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2)ora vorrei fare un metodo che mi restituisca un array per poi lavorarlo ho provato a fare una prova cosi:
codice:
public class ProvaRestituzione {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ProvaRestituzione p = new ProvaRestituzione();
ArrayList stamparray = new ArrayList();
stamparray= p.restituisciArray();
for (int i = 0; i <stamparray.size(); i++) {
System.err.println(p.restituisciArray().get(i));
}
}
public ArrayList<String> restituisciArray (){
ArrayList gino = new ArrayList();
ArrayList<String> arrayBusta = new ArrayList<String>();
try {
Socket socket = new Socket("192.168.1.67",6789);
try {
ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream()); //Error Line!
try {
Object object = objectInput.readObject();
arrayBusta = (ArrayList<String>) object;
for (int i = 0; i < arrayBusta.size(); i++) {
// String get = arrayBusta.get(i);
// System.out.println(arrayBusta.get(i));
gino.add(arrayBusta.get(i));
}
} catch (ClassNotFoundException e) {
System.out.println("The title list has not come from the server");
e.printStackTrace();
}
} catch (IOException e) {
System.out.println("The socket for reading the object has problem");
e.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return gino;
}
Ma ottengo questo errore:
codice:
The socket for reading the object has problemjava.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2320)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2333)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2804)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at provarestituzione.ProvaRestituzione.restituisciArray(ProvaRestituzione.java:49)
at provarestituzione.ProvaRestituzione.main(ProvaRestituzione.java:35)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at provarestituzione.ProvaRestituzione.main(ProvaRestituzione.java:35)
C:\Users\Paolo\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 8 seconds)
Dove Sbaglio?