流损坏异常 - 代码类型 AC [java] 无效



我正在尝试开发一个Java客户端-服务器应用程序,其中多个客户端可以连接到服务器。我为每个传入的客户端启动一个单独的线程,将客户端套接字传递给线程,然后打开一对流来保持通信。我得到了这个java.io.StreamCorruptedException - 无效的代码类型AC。

根据我的研究,这意味着我使用新的ObjectOutputStream来写入现有的ObjectInputStream。但是,我不太确定如何在代码中处理这个问题,尝试了各种地方进行流初始化,但我仍然没有做对。发生异常的行在客户端代码中标记。我可能对 IO 流缺乏一些基本的了解。这是我的服务器代码,以及运行客户端连接的线程:

 public class Server {
 public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listeningSocket = true;
        try {
            serverSocket = new ServerSocket(11111);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 11111");
        }
        while(listeningSocket){
            System.out.println("Waiting for a client to connect...");
            Socket clientSocket = serverSocket.accept();
            System.out.println("Client connected!");
            ConnectThread ct = new ConnectThread(clientSocket);
            ct.start();
            ct.run();
        }
        serverSocket.close();       
    }

}

连接线程:

public class ConnectThread extends Thread{
private Socket socket = null;

public ConnectThread(Socket socket) {
    super("ConnectThread");
    this.socket = socket;
}
@Override
public void run(){
    ObjectOutputStream serverOutputStream = null;
    ObjectInputStream serverInputStream = null;
    try {
        System.out.println("check");
        serverOutputStream = new ObjectOutputStream(socket.getOutputStream());
        System.out.println("check");
        serverInputStream = new ObjectInputStream(socket.getInputStream());
        serverOutputStream.writeInt(42);
        System.out.println("check");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
        try {
            serverOutputStream.close();
            serverInputStream.close();              
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
        //implement your methods here

}

和客户端:

public class Client {
public static void main(String[] arg) {

    Socket socketConnection = null;
    ObjectOutputStream clientOutputStream = null;
    ObjectInputStream clientInputStream = null;
            try {
        socketConnection = new Socket("127.0.0.1", 11111);
        clientOutputStream = new ObjectOutputStream(
                socketConnection.getOutputStream());
        clientInputStream = new ObjectInputStream(
                socketConnection.getInputStream());
        System.out.println("check");
        System.out.println(clientInputStream.readInt()); // HERE'S WHERE THE EXCEPTION OCCURS
    } catch (Exception e) {
        System.out.println("The following exception has occured and was caught:");
        System.out.println(e);
    }
    finally{
        try {
            clientOutputStream.close();
            clientInputStream.close();
            socketConnection.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

您需要先修复此位:

        ConnectThread ct = new ConnectThread(clientSocket);
        ct.start();
        ct.run();

您的 ConnectThread 是一个子类 if 线程,一旦您调用 start,它将启动一个新线程来执行您的运行方法。所以删除 ct.run();并再次检查。您很可能尝试从同一套接字的两个线程(接受一个和连接一个)读取,这会导致失败。

相关内容

  • 没有找到相关文章

最新更新