开发者

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?


It seems like the problem might be the same as the one described here.


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 .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜