Java StreamCorruptedException尝试从服务器向所有客户端广播对象

时间:2015-11-18 13:07:57

标签: java multithreading sockets objectoutputstream

当我尝试将多个客户端连接到我的服务器时,我收到错误" java.io.StreamCorruptedException:无效的类型代码:AC"在ClientThread类的第60行。

我试图将每个客户端发送的任何对象广播到所有客户端。在这种情况下,我只是发送一个带有1个变量的对象" x"值为5.我想这样做,以便当客户端加入时,它将对象发送到连接到服务器的所有客户端。 现在,当我刚加入1个客户端时,它会工作,它会连接并将值发送回客户端。

每当我加入第二个客户端时,它会在第一个加入的客户端上给我错误。 我做了一些研究,发现由于有多个输出流或类似的东西,但我无法找到如何将解决方案应用于我的问题:(

我此时真的迷失了,并且不知道还能去哪里但是请尝试在这里获得帮助。

如果有人能帮助我,我真的很感激,因为这样做无法继续使用我的代码真的很令人沮丧。

这是班级:

服务器:

public class Server {

    protected ServerSocket serverSocket = null;
    protected boolean listening = true;
    protected List<Socket> clientSockets;

    public Server(int port) throws IOException {
        serverSocket = new ServerSocket(4444);
        clientSockets = new ArrayList<>();

        while (listening) {
            Socket clientSocket = serverSocket.accept();
            clientSockets.add(clientSocket);
            Thread clientThread = new ServerThread(clientSocket, this);
            clientThread.start();
        }
        serverSocket.close();
    }

    public void broadcast(ObjectPackage outputObject) {
        for (Socket socket : clientSockets) {
            ObjectOutputStream out;
            try {
                out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(outputObject);
                out.flush();
            } catch (IOException e) {
                // We failed at writing to the socket...
                //e.printStackTrace();
                //removeSocket(socket);
            }
        }
    }

    public void removeSocket(Socket socket) {
        clientSockets.remove(socket);
        System.out.println("socket removed");
    }

    public static void main(String[] args) throws IOException {
        new Server(4444);
    }

}

ServerThread:

public class ServerThread extends Thread{

    private Socket socket = null;
    private Server server;

    public ServerThread(Socket socket, Server server) throws IOException {
        this.socket = socket;
        this.server = server;
    }

    public void run() {
        try {

            ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());

            ObjectPackage objectOut,objectIn;

            while ((objectIn  = (ObjectPackage) objIn.readObject()) != null) {

                objectOut = objectIn;
                System.out.println("Object received: " + objectOut.x);
                server.broadcast(objectOut);
            }
        }
        catch (EOFException e){

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

    }
}

ClientThread

public class ClientThread {

    private String host;
    private int port;
    private ObjectPackage pack;
    private Socket socket;

    public ClientThread(final String host, final int port) {
        this.host = host;
        this.port = port;
    }

    public void connect() {
        try {
            socket = new Socket(host, port);

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.format("[Client] Connected to server %s:%d!\n", host, port);

        final Thread outThread = new Thread() {
            @Override
            public void run() {
                System.out.println("Started...");
                ObjectOutputStream obj;
                try {
                    obj = new ObjectOutputStream(socket.getOutputStream());
                    pack = new ObjectPackage(5);
                    obj.writeObject(pack);

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

        final Thread inThread = new Thread() {
            @Override
            public void run() {


                try {
                    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                    ObjectPackage object;

                    while (true) {
                        object = (ObjectPackage) in.readObject();//line 60
                        System.out.println(object.x);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        inThread.start();
    }

    public void disconnect() {
        try {
            socket.close();
            System.out.println("socked closed");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

我还有2个其他类,但它只是我发送的序列化对象和启动客户端的主类。

0 个答案:

没有答案