简体   繁体   中英

Problem with Chat client program

I have a server chat and client chat programs running on localhost. When I try to connect to the server my client program freezes on next line in = new ObjectInputStream(socket.getInputStream()); here is a piece of code where I try to connect to the server

            Socket socket = new Socket(host, port);
            try {
                out = new ObjectOutputStream(socket.getOutputStream());
                in = new ObjectInputStream(socket.getInputStream());
                Message m = new Message(null, nick, Message.Type.REGISTER);
                out.writeObject(m);
                out.flush();
            } catch (IOException ex) {
                socket.close();
                throw ex;
            }

Message class implements Serializable interface, so it can be serialized over the network. And here is a piece of code where server hadle client request

try {
            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
            Message m = (Message) in.readObject();
            switch (m.getMessageType()) {
                case REGISTER:
                    registerUser(m);
                    break;
                case CHATMESSAGE:
                    sendMessageToAll(m);
                    break;
                case UNREGISTER:
                    unregisterUser(m);
                    break;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Chatserver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Chatserver.class.getName()).log(Level.SEVERE, null, ex);
        }

methods registerUser, unregisterUser, sendMessageToAll simply call next method

private void sendMessage(Message m, Socket s) throws IOException {
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
        out.writeObject(m);
        out.flush();
       // out.close();
    }

Where is a mistake?

看来问题可能与此处描述的问题相同

Just faced this problem .. So giving the answer in this thread itself :

ObjectOutputStream writes a stream header when we create it (new ObjectOutputStream(out))

Similarly , ObjectInputStream , when we create it (new ObjectInputStream(in)) , tries to read the same header from the corresponding ObjectOutputStream at the server side

Here , in client ,

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

the ObjectInputStream created blocks when trying to read the stream header , which will not come since there is no corresponding ObjectOutputStream at server which will write the header to the client .

The problem is not just this . If the ObjectOutputStream creation at one side aligns with some other reads at the client side which is supposed to read something of our choice , it may read the stream header instead of the actual value and end up in an incorrect value .

Solution : The ObjectOutputStream and the ObjectInputStream created at the client and server sides must align with each other .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM