当前位置: 代码迷 >> J2SE >> 纠结JAVA SOCKET 错误:java.io.StreamCorruptedException: invalid stream header
  详细解决方案

纠结JAVA SOCKET 错误:java.io.StreamCorruptedException: invalid stream header

热度:83   发布时间:2016-04-24 00:53:08.0
纠结JAVA SOCKET 异常:java.io.StreamCorruptedException: invalid stream header
异常信息:(客户端发出信息,第一次服务端接收正常,再发服务端就抛出这个异常了,我换成Data类型就可以了,但是我要传的是对象,所以要用Object类型。烦请高手看下。)
java.io.StreamCorruptedException: invalid stream header: 74000161
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at com.nothing.Lab.S$Clients.run(S.java:55)
at java.lang.Thread.run(Thread.java:619)

服务端:(第二次接收时抛异常)
Java code
package com.nothing.Lab;import java.io.DataInputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.ServerSocket;import java.net.Socket;public class S {        public ServerSocket serverSocket = null;    public ObjectInputStream ois = null;    public ObjectOutputStream oos = null;    public boolean started = false, bConnect = false;        public static void main(String[] args){        new S().go();    }        public void go(){        try {            serverSocket = new ServerSocket(8888);            started = true;        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        try{            while(started){                Socket s = serverSocket.accept();                System.out.println("a clients in");                Clients c = new Clients(s);                bConnect = true;                new Thread(c).start();            }        }catch(IOException e){            e.printStackTrace();        }    }        public class Clients implements Runnable{        Socket s;        String str = "";        public Clients(Socket s){            this.s = s;        }        @Override        public void run() {            // TODO Auto-generated method stub            try{                while(bConnect){                    ois = new ObjectInputStream(s.getInputStream());    //第二次接收时抛异常                    str = ois.readObject().toString();                    System.out.println(str);                }            }catch(ClassNotFoundException e1){                e1.printStackTrace();            }catch(IOException e){                System.out.println("quit");                e.printStackTrace();            }finally{                                    try {                        if(ois != null)                            ois.close();                        if(s != null)                            s.close();                    } catch (IOException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }            }        }            }}


客户端:
C/C++ code
package com.nothing.Lab;import java.awt.BorderLayout;public class C extends JFrame {    public Socket socket = null;    public ObjectInputStream ois = null;    public ObjectOutputStream oos = null;    private JPanel contentPane;    private JTextField textField;    /**     * Launch the application.     */    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    C frame = new C();                    frame.go();                    frame.setVisible(true);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }    /**     * Create the frame.     */    public C() {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setBounds(100, 100, 308, 300);        contentPane = new JPanel();        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));        contentPane.setLayout(new BorderLayout(0, 0));        setContentPane(contentPane);                textField = new JTextField();        contentPane.add(textField, BorderLayout.SOUTH);        textField.setColumns(10);                JTextArea textArea = new JTextArea();        contentPane.add(textArea, BorderLayout.CENTER);                textField.addActionListener(new EnterDown());    }        public void go(){        try{            socket = new Socket("127.0.0.1", 8888);            oos = new ObjectOutputStream(socket.getOutputStream());            System.out.println("connection");        }catch(IOException e){            e.printStackTrace();        }            }        public class EnterDown implements ActionListener{        public void actionPerformed(ActionEvent e){            try {                                String str = textField.getText();                System.out.println("c:"+str);                oos.writeObject(new String("a"));//                oos.flush();//                oos.close();            } catch (IOException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }        }    }        public class recv implements Runnable{        @Override        public void run() {            // TODO Auto-generated method stub                    }            }}
  相关解决方案