简体   繁体   English

聊天客户端程序问题

[英]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()); 当我尝试连接到服务器时,我的客户端程序冻结in = new ObjectInputStream(socket.getInputStream());下一行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. 消息类实现了Serializable接口,因此可以通过网络对其进行序列化。 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 方法registerUser,unregisterUser,sendMessageToAll只需调用下一个方法

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)) 创建对象时,ObjectOutputStream会写一个流头(新的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 类似地,当我们创建ObjectInputStream(new ObjectInputStream(in))时,它尝试从服务器端的相应ObjectOutputStream读取相同的标头。

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 . 当尝试读取流头时,ObjectInputStream创建了块,因为服务器上没有相应的ObjectOutputStream会将头写入客户端,所以它不会出现。

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 . 如果在一侧的ObjectOutputStream创建与在客户端进行的其他读取对齐(应该读取我们选择的内容),则它可能读取流头而不是实际值,并最终得出错误的值。

Solution : The ObjectOutputStream and the ObjectInputStream created at the client and server sides must align with each other . 解决方案:在客户端和服务器端创建的ObjectOutputStream和ObjectInputStream必须彼此对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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