java.io.StreamCorruptedException:无效的流标头:75720002 [英] java.io.StreamCorruptedException: invalid stream header: 75720002

查看:81
本文介绍了java.io.StreamCorruptedException:无效的流标头:75720002的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个服务器-客户端应用程序,其中服务器将pdf文件发送到所有连接的客户端.问题是我遇到此错误,我正在寻找解决方案,但无法获取任何解决方案.这是错误

  java.io.StreamCorruptedException:无效的流标头:75720002在java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)在java.io.ObjectInputStream处.< init>(ObjectInputStream.java:279)在StudentThread.run(StudentThread.java:102) 

这是服务器发送代码:

  public void run(){尝试{字符串ModifySentence;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);//字符串语句;System.out.println("TeachID." + id);modifiedSentence = in.readLine();System.out.println(学生" + id +说:" + modifiedSentence);arrS = modifiedSentence.split(");out.println("H​​ello" + arrS [2] +," + id);studName = arrS [2];((默认列表模型)Teacher.made_list.getModel()).addElement(studName);while(true){modifiedSentence = in.readLine();arrS = modifiedSentence.split(");if(arrS [0] .equals("AcceptFile")){尝试{ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());byte []缓冲区=(byte [])ois.readObject();字符串pic ="copyServer" + id +.pdf";System.out.println(pic);FileOutputStream fos = new FileOutputStream(pic);fos.write(缓冲区);fos.flush();fos.close();}catch(异常e){System.out.println(例外写作");}}}捕获(IOException e){}最后{尝试{socket.close();}catch(IOException e){}}}公共无效sendF(字符串fn,老师教){尝试{out.println("AcceptFile,");ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());FileInputStream fis =新的FileInputStream(fn);byte []缓冲区=新的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教学){out.println(elem);//System.out.println(线程ID为" + this.id);System.out.println(this.socket.getInetAddress());} 

客户在这里收到代码:

  public void run(){尝试{out = new PrintWriter(socket.getOutputStream(),true);out.println("H​​ello Server" + name +,");字符串ModifySentence;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 []缓冲区=(byte [])ois.readObject();字符串pic ="copyServer" + studId +.gif";System.out.println(pic);FileOutputStream fos = new FileOutputStream(pic);fos.write(缓冲区);fos.flush();fos.close();}}}catch(异常e){e.printStackTrace();}} 

解决方案

BufferedReader 可以缓冲来自套接字的数据量超过您尚未读取的数据.因此,在客户端,您的 byte [] 的标头可能已经由 inFromServer 读取器读取并缓冲了,并且对 ObjectInputStream不可用..

不要那样做.要么手动"完成所有封送处理(使用 PrintWriter / BufferedReader 对),要么全部完成对象序列化,并使用 ObjectOutputStream / ObjectInputStream .

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();
    }
}

解决方案

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.

这篇关于java.io.StreamCorruptedException:无效的流标头:75720002的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆