新ServerSocket上的异常:java.io.StreamCorruptedException:无效的流标头

时间:2014-07-21 06:46:44

标签: java android sockets

我试图通过OutPutStream上的文件发送一些详细信息,但是我收到了这个例外:

 Exception on new ServerSocket: java.io.StreamCorruptedException: invalid stream header: 
 00BABB8D

这是我的客户端代码:

ObjectInputStream  sInput = null;       // to read from the socket
ObjectOutputStream sOutput = null;     // to write on the socket
Socket socket = null;

try {  
           Log.i("Images","in the async");  
           socket = new Socket(HOST, 1600);  
           System.out.println(socket);  
           System.out.println("Connecting...");  
           Log.i("Images","in the service"+filepath+"");    
           Log.i("Images","filepath in the async "+""+filepath+"");
           File fil=new File(filepath);  
             System.out.println(fil);


               System.out.println(fil.getName());  


           OutputStream os = socket.getOutputStream();    
           DataOutputStream dos = new DataOutputStream(os);   


           dos.writeInt(1);  

                 dos.writeUTF(fil.getName());  


                 int filesize = (int) fil.length();  
                 dos.writeInt(filesize);  

                 ObjectOutputStream oos = new ObjectOutputStream(dos);
                 oos.writeObject("Hey !");
                  // Here is the packet im trying to send along

           byte [] buffer = new byte [filesize];  

           FileInputStream fis = new FileInputStream(fil.toString());    
           BufferedInputStream bis = new BufferedInputStream(fis);    

           //Sending file name and file size to the server    
           bis.read(buffer, 0, buffer.length); //This line is important  
           dos.write(buffer, 0, buffer.length);     

           fis.close();
           dos.flush();   
          .
          .
          .

和我的ServerSide:

   int fileSize = 0;
                        try {
                            fileSize = clientData.read();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }        
                         fileSize = (fileSize > 0) ? fileSize :    
 0;                                                                      // guard   
 against 
 negatives.
                        List<File> files = new ArrayList<>(fileSize);          
 //store     list of filename from client directory        // Using List and <>
                        List<Integer> sizes = new ArrayList<>(fileSize);        
 //store     file size from client 
                      //Start to accept those filename from server

                        for (int count=0;count < fileSize;count ++){  
                            File ff = null;
                            try {
                                ff = new File(clientData.readUTF());
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }  
                            files.add(ff);  

                            ObjectInputStream ois = new ObjectInputStream(in);
                            try {
                                String Request = (String) ois.readObject();
                            } catch (ClassNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            //here is where im trying to read the packet

                            System.out.println(Request);

我很感激,如果有人能帮我解决我做错了什么?

1 个答案:

答案 0 :(得分:0)

不要使用多种类型的输入和输出流。使用对象流来处理所有事情。这有很多原因,但在这种情况下,问题是在对话过程中引入了ObjectOutputStream。