java.io.StreamCorruptedException:类型代码无效:04

时间:2014-12-27 13:42:47

标签: java ios

我的客户端 - 服务器应用程序有点问题。当我想连接多个客户端并发送smth,或者我在我的客户端进行注销并再次尝试连接时,我得到了例外: " java.io.StreamCorruptedException:无效的类型代码:04"

问题是什么?感谢您的帮助。

服务器代码:

class ClientCommunication implements Runnable {
    private Socket incoming;

    public ClientCommunication(Socket clientSocket) {
        incoming = clientSocket;
    }

    public void run() {
        try {
            synchronized (this) {                                       
                    serverObjectOutput = new ObjectOutputStream(
                            incoming.getOutputStream());
                    serverObjectInput = new ObjectInputStream(
                            incoming.getInputStream());                      
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        int operation = -1;
        synchronized(this) {
            while (true) {
                try{                        
                    if(serverObjectInput.available() > 0){
                    operation = serverObjectInput.readInt();

                    switch(operation) {
                    case 1:
                            Employee employee = (Employee) serverObjectInput.readObject();
                            //CHECK LOGGING DATA
                            // SEND RESULT = 1 OR RESULT = -1
                            break;
                }
              }
            } catch(IOException | ClassNotFoundException | SQLException ex)                 
            {   
                ex.printStackTrace();
            }                   
          }
        }           
    }
}


class ServerStart implements Runnable {
    private int portNumber;

    public ServerStart(int portNumber) {
        this.portNumber = portNumber;
    }

    public void run() {

        try {
            conn = getConnection();
            stat = conn.createStatement();

        } catch (SQLException e1) {             
            e1.printStackTrace();
        } catch (IOException e1) {              
            e1.printStackTrace();
        } catch (InterruptedException e) {              
            e.printStackTrace();
        }

        try {
            serverSocket = new ServerSocket(portNumber);        

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

        try {
            while (true) {
                Socket incoming = serverSocket.accept();

                Runnable r = new ClientCommunication(incoming);
                Thread t = new Thread(r);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

客户端功能:

            public void actionPerformed(ActionEvent e) {
                if (isConnected == false) {
                    String ServerIP = ip.getText().trim();

                    int ServerPort = Integer
                            .parseInt(port.getText().trim());


                    try {
                        ClientSocket = new Socket(ServerIP, ServerPort);                        

                        clientObjectInput = new ObjectInputStream(
                                ClientSocket.getInputStream());
                        clientObjectOutput = new ObjectOutputStream(
                                ClientSocket.getOutputStream());

                        isConnected = true;
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    synchronized (this) {
                        try {                               
                            ClientLoginFrame login = new ClientLoginFrame();

                            Employee employee = login.getEmployee();                                                                
                            clientObjectOutput.writeInt(1);
                            clientObjectOutput.flush();
                            clientObjectOutput.writeObject(employee);                               
                            int result = clientObjectInput.readInt();

                            if(result == 1)
                            {                           
                             // DO SMTH
                            }
                            else { 
                                isConnected = false;
                                ClientSocket.close();                                   
                            }                           
                        } catch (IOException ex) {
                            ex.printStackTrace();       
                        }
                    }
                }
            }
        });

1 个答案:

答案 0 :(得分:0)

我怀疑您的问题是您在连接之间共享单身serverInputStreamserverOutputStream。这不是问题,除非您有多个线程,此时在多个线程中同时使用相同的流会破坏流(或使其读取无效)