2012-10-15 15 views
6

Sto provando a inviare un oggetto dal socket lato server al socket lato client su TCP. Non riesco a scoprire dov'è il problema.Invio di un arraylist <String> dal lato server al lato client su TCP tramite socket?

Qui è l'errore che sto ottenendo sul lato client:

java.io.EOFException 
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280) 
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749) 
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779) 
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279) 
    at ClientSide.main(ClientSide.java:16) 

Codice per il lato server:

import java.io.*; 
import java.net.*; 
import java.util.ArrayList; 

public class ServerSide { 

    public static void main(String[] args) { 
     try 
     { 
      ServerSocket myServerSocket = new ServerSocket(9999); 
      Socket skt = myServerSocket.accept(); 
      ArrayList<String> my = new ArrayList<String>(); 
      my.set(0,"Bernard"); 
      my.set(1, "Grey"); 
      try 
      { 
       ObjectOutputStream objectOutput = new ObjectOutputStream(skt.getOutputStream()); 
       objectOutput.writeObject(my);    
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

} 

Codice per il lato client:

import java.io.*; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.ArrayList; 


public class ClientSide { 

    public static void main(String[] args) 
    { 
     try {  
      Socket socket = new Socket("10.1.1.2",9999); 
      ArrayList<String> titleList = new ArrayList<String>(); 
      try { 
       ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream()); //Error Line! 
       try { 
        Object object = objectInput.readObject(); 
        titleList = (ArrayList<String>) object; 
        System.out.println(titleList.get(1)); 
       } 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(); 
     } 
    } 
} 
+0

Solo un consiglio: non inviare oggetti che fai su internet. Invia solo tipi primitivi. String, Byte, Integer, Float, Double, Char e quindi convertire su entrambi i lati nel formato desiderato. – OmniOwl

+0

inviato più stringhe e quindi ricreare l'arraylist, che è meglio – Gianmarco

+0

@ Gianmarco: Questa è una buona idea, ma voglio ancora imparare come inviare un oggetto ed è anche parte delle specifiche di assegnazione! – Bernard

risposta

5

Cambiare da set a add fa il trucco

ArrayList<String> my = new ArrayList<String>(); 
my.add("Bernard"); 
my.add("Grey"); 

ps. come consigliato dagli altri questa non è una buona idea, ma usa solo per l'apprendimento

+0

Dopo 1 ora di pubblicazione di questo messaggio ho scoperto che il problema era il metodo impostato dopo averlo modificato in quello che hai detto che il programma funzionava al 100%. – Bernard

+0

Mi chiedo, non dovrebbe il tuo codice server generare una IndexOutOfBoundsException dal momento che stai chiamando set per elementi inesistenti? –

+0

@KlitosKyriacou: Purtroppo no. Non ha lanciato alcuna eccezione, quindi ho perso le mie 5 ore di tempo! – Bernard

Problemi correlati