Server Socket Java发送了几条消息,但Client收到的消息总是一样的

时间:2014-06-21 20:31:50

标签: java sockets client

我遇到服务器/客户端套接字问题。服务器向客户端发送多个对象,但客户端始终具有接收的第一个消息,而不接收另一个消息。如果我在客户端打印一条消息,它会收到消息,但是如果我打印这些消息,它们总是一样的。但是,如果我在服务器端打印消息,则消息不同。你能帮助我吗?我已经看到,如果我发送Integer一切正常。只有我必须发送的对象才有问题。这是服务器和客户端的代码(它们链接到GUI和控制器):

服务器

    @Override
public void update(Observable o, Object arg) {

        if(o instanceof ModelChanges){
            arrayPlayersSocket = socketServerCreate.getArrayPlayerSocket();
            Iterator<ObjectOutputStream> arrayIterator = arrayPlayersSocket.iterator();
            while(arrayIterator.hasNext()){
                outputMessage = (ObjectOutputStream)arrayIterator.next();
                System.out.println(((ModelChanges)arg).getAzione());
                try {
                    outputMessage.writeObject(arg);
                    outputMessage.flush();
                    /*if(arrayPlayersSocket.indexOf(outputMessage) != controller.getTurn().getCurrentPlayer()){
                        ModelChanges modelChanges = new ModelChanges();
                        modelChanges.spegniBottoniOnLine(false, false, false);
                        outputMessage.writeObject(modelChanges);
                        outputMessage.flush();
                    }*/
                } catch (IOException e) {
                    System.out.println("IOException while the server sends an object to the client!");
                    return;
                }
            }
        }

}

客户端

    @Override
public void run() {

    while(true){
        try {
            message = inputMessage.readObject();
            if(message instanceof Integer){
                socketClientCreate.setArrayIndex((Integer)message);
            }else if(message instanceof ModelChanges){
                ModelChanges modelChanges = (ModelChanges)message;
                view.getMappa().repaintView(modelChanges);
            }

        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException waiting a message from the server!\n"+e.getStackTrace());
            return;
        } catch (IOException e) {

        }
    }

}

0 个答案:

没有答案