Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.StreamCorruptedException: invalid stream header: 75720002

Tags:

java

i am creating a server-client application where the server sends a pdf file to the all connected clients. The problem is i get this error and i searched for a solution but couldn`t get any. This is the error

 java.io.StreamCorruptedException: invalid stream header: 75720002
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
    at StudentThread.run(StudentThread.java:102)

Here is the server sending code:

public void run()
{
    try
    {
        String modifiedSentence;
        in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        inFromServer = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())), true);
        //String sentence;
        System.out.println("TeachID. "+id);
        modifiedSentence = in.readLine();
        System.out.println("Student "+id+" says:"+modifiedSentence);
        arrS=modifiedSentence.split(" ");
        out.println("Hello "+arrS[2]+","+id);   
        studName=arrS[2];
        ((DefaultListModel) Teacher.made_list.getModel()).addElement(studName);

        while( true )
        {
            modifiedSentence = in.readLine();
            arrS=modifiedSentence.split(" ");
            if(arrS[0].equals("AcceptFile"))
            {
                try
                {
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    byte[] buffer = (byte[])ois.readObject();
                    String pic="copyServer"+id+".pdf";
                    System.out.println(pic);
                    FileOutputStream fos = new FileOutputStream(pic);
                    fos.write(buffer);  
                    fos.flush();
                    fos.close();
                }
                catch(Exception e)
                {
                    System.out.println("Exception writing");
                }

            }

    }
    catch (IOException e)
    {
    }
    finally
    {
        try
        {
            socket.close();
        }
        catch(IOException e)
        {
        }
    }
}
public void sendF(String fn,Teacher teach)
{
    try{
        out.println("AcceptFile,");
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()) ;
        FileInputStream fis = new FileInputStream(fn);
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        //ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()) ;
        oos.writeObject(buffer);
        oos.flush();
        fis.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
}
public void sendThread(String elem, Teacher teach)
{

    out.println(elem);
    //System.out.println ("Thread id is " + this.id);
    System.out.println(this.socket.getInetAddress());
}

Here is the client receiving code:

public void run() 
{
    try
    {

        out=new PrintWriter(socket.getOutputStream(), true);
        out.println("Hello Server "+name+",");

        String modifiedSentence;
        BufferedReader inFromServer = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
        modifiedSentence = inFromServer.readLine();
        System.out.println( modifiedSentence );

        arrT=modifiedSentence.split(",");
        if(arrT[0].equals("Hello "+name))
        {
            studId=Integer.parseInt(arrT[2]);
            System.out.println("My Id="+studId);
        }

        while( true )
        {
            modifiedSentence = inFromServer.readLine();
            System.out.println( modifiedSentence );

            arrT=modifiedSentence.split(",");
            if(arrT[0].equals("AcceptFile"))
            {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                byte[] buffer = (byte[])ois.readObject();
                String pic="copyServer"+studId+".gif";
                System.out.println(pic);
                FileOutputStream fos = new FileOutputStream(pic);
                fos.write(buffer);  
                fos.flush();
                fos.close();
            }
    }
}
catch( Exception e )
    {
        e.printStackTrace();
    }
}
like image 220
Fatema Mohsen Avatar asked Jun 11 '11 09:06

Fatema Mohsen


1 Answers

BufferedReader can buffer more data from the socket than you've read yet. So on the client side, the header of your byte[] has probably already been read and buffered by your inFromServer reader, and will not be available to your ObjectInputStream.

Don't do that kind of thing. Either do all your marshaling "manually" (using the PrintWriter/BufferedReader pair), or do it all with object serialization with ObjectOutputStream/ObjectInputStream.

like image 135
Mat Avatar answered Oct 20 '22 15:10

Mat