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

时间:2014-12-31 12:13:44

标签: java multithreading io stream server

我正在尝试开发一个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();
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

您需要先修复此位:

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

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