java.io.StreamCorruptedException:无效的types代码:04

我的客户端 – 服务器应用程序有一点问题。 当我想连接多个客户端并发送smth,或者在我的客户端注销并尝试再次连接时,我得到exception:“java.io.StreamCorruptedException:无效的types代码: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(); } } 

客户端function:

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

我怀疑你的问题是,你是共享连接之间的单例serverInputStreamserverOutputStream 。 这不是一个问题,直到你有多个使用同一个stream的多个线程同时在一个点上破坏stream(或使其无效)