2010-04-12 8 views
9

im writen un'applicazione client-server, e ora sto di fronte a un problema che non so come risolvere:Java intestazione flusso non valida Problema

Questo è il client:

try 
     { 

      Socket socket = new Socket(ip, port); 


      ObjectOutputStream ooos = new ObjectOutputStream(socket 
        .getOutputStream()); 
      SendMessage message = new SendMessage(); 

      message.numDoc = value.numDoc; 
      message.docFreq = value.docFreq; 

      message.queryTerms = query; 
      message.startIndex = startIndex; 
      message.count = count; 
      message.multiple = false; 
      message.ips = null; 
      message.ports = null; 

      message.value = true; 
      message.docFreq = value.docFreq; 
      message.numDoc = value.numDoc; 
      ooos.writeObject(message); 


      ObjectInputStream ois = new ObjectInputStream(socket 
        .getInputStream()); 
      ComConstants mensajeRecibido; 
      Object mensajeAux; 
      String mensa = null; 

      byte[] by = null; 

      do 
      { 

       mensajeAux = ois.readObject(); 

       if (mensajeAux instanceof ComConstants) 
       { 


        System.out.println("Thread by Thread has Search Results"); 

        String test; 

        ByteArrayOutputStream testo = new ByteArrayOutputStream(); 

        mensajeRecibido = (ComConstants) mensajeAux; 

        byte[] wag; 

        testo.write(
          mensajeRecibido.fileContent, 0, 
          mensajeRecibido.okBytes); 

        wag = testo.toByteArray(); 


        if (by == null) { 

         by = wag; 

        } 
        else { 

         int size = wag.length; 

          System.arraycopy(wag, 0, by, 0, size); 
        } 


       } else 
       { 

        System.err.println("Mensaje no esperado " 
          + mensajeAux.getClass().getName()); 
        break; 
       } 
      } while (!mensajeRecibido.lastMessage); 




      //ByteArrayInputStream bs = new ByteArrayInputStream(by.toByteArray()); // bytes es el byte[] 
      ByteArrayInputStream bs = new ByteArrayInputStream(by); 
      ObjectInputStream is = new ObjectInputStream(bs); 
      QueryWithResult[] unObjetoSerializable = (QueryWithResult[])is.readObject(); 
      is.close(); 

      //AQUI TOCARIA METER EL QUICKSORT 

      XmlConverter xce = new XmlConverter(unObjetoSerializable, startIndex, count); 
      String serializedd = xce.runConverter(); 



     tempFinal = serializedd; 

      ois.close(); 
      socket.close(); 

     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     i++; 

     } 

E questo è il mittente:

try 
    { 

     QueryWithResult[] outputLine; 

     Operations op = new Operations(); 

     boolean enviadoUltimo=false; 

     ComConstants mensaje = new ComConstants(); 
     mensaje.queryTerms = query; 

     outputLine = op.processInput(query, value); 

     //String c = new String(); 
     //c = outputLine.toString(); 
     //StringBuffer swa = sw.getBuffer(); 

     ByteArrayOutputStream bs= new ByteArrayOutputStream(); 

     ObjectOutputStream os = new ObjectOutputStream (bs); 
     os.writeObject(outputLine); 
     os.close(); 

     byte[] mybytearray = bs.toByteArray(); 

     ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mybytearray); 

     BufferedInputStream bis = new BufferedInputStream(byteArrayInputStream); 

     int readed = bis.read(mensaje.fileContent,0,4000); 


     while (readed > -1) 
     { 


      mensaje.okBytes = readed; 


      if (readed < ComConstants.MAX_LENGTH) 
      { 
       mensaje.lastMessage = true; 
       enviadoUltimo=true; 
      } 
      else 
       mensaje.lastMessage = false; 

      oos.writeObject(mensaje); 


      if (mensaje.lastMessage) 
       break; 

      mensaje = new ComConstants(); 
      mensaje.queryTerms = query; 

      readed = bis.read(mensaje.fileContent); 
     } 

     if (enviadoUltimo==false) 
     { 
      mensaje.lastMessage=true; 
      mensaje.okBytes=0; 
      oos.writeObject(mensaje); 
     } 

     oos.close(); 
    } catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

E questo è il log degli errori:

Thread by Thread has Search Results 
java.io.StreamCorruptedException: invalid stream header: 20646520 
at java.io.ObjectInputStream.readStreamHeader(Unknown Source) 
at java.io.ObjectInputStream.<init>(Unknown Source) 
at org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747) 
at org.tockit.comunication.ServerThread.run(ServerThread.java:129) 
at java.lang.Thread.run(Unknown Source) 

Dove a org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747) è questa linea ObjectInputStream is = new ObjectInputStream(bs); al 1 ° codice appena dopo while (!mensajeRecibido.lastMessage);

Tutte le idee?

risposta

7

Il valore 20646520 è in ASCII @A.

ObjectInput/OutputStreams utilizzano un valore "magico" all'inizio del flusso, per indicare che è conforme alla serializzazione speciale degli oggetti. (Penso che fosse 0xCAFEBABE, ma non sono sicuro)

Ciò significa nella tua situazione che qualcosa ha già letto il flusso prima che ObjectInputStream abbia la possibilità di leggere la magia o che il flusso che legge non sia prodotto da un ObjectOutputStream;

si assegna la variabile by a wig (o aggiungere), che è un array di byte che non è generato da un ObjectOutputStream, per quanto posso dire, in quanto utilizza mensajeRecipido.fileContent. Presumo che mensajeRecipido.fileContent sia il contenuto di un file reale. In questa istanza in esecuzione non è dello stesso formato di ObjectOutputStream ed è per questo che si ottiene l'eccezione dell'intestazione dello stream.

+0

Grazie per la risposta. mensajeRecibido.filecontet proviene dal codice del mittente, il suo contenuto dei messaggi che invio. Penso che sia dello stesso formato dal mio punto di vista, ma è molto probabile che non sottovaluto la tua affermazione o sappia cosa dovrei fare. Grazie: D –

+0

Non ho ancora idea di come risolvere il problema: S –

+0

"Cafe Babe", bella lol – Xenalin

Problemi correlati