Java Event仅在我第一次触发它时才起作用

时间:2016-10-16 13:16:10

标签: java swing client-server

我正在编写聊天服务器和客户端应用程序,我的身份验证部分出现问题。如果你做得对,它会正常登录,如果你没有,你会得到正确的答案,但我永远不能让它允许用户在第一次失败时再次尝试登录。我在代码部分或事件监听器部分中移动读取和写入服务器方面做了一些更改,但无济于事。目前我第二次点击按钮似乎只是完全跳过从服务器发送或读取,因为客户端的receivedSignal在第一次触发后永远不会改变,并且总是触发相同的错误消息。

服务器部分

while (true) {
    loginUser = (User) in.readObject();
    int signal = authenticator.authenticate(loginUser);
    out = new ObjectOutputStream(client.getOutputStream());

    if (signal > 0) {
        for (User temp : users) {
            if (temp.getId().equalsIgnoreCase(loginUser.getId()) && signal != 2) {
                System.out.println(loginUser.getId() + " is already online!");
                out.writeObject(new Message("server", Integer.toString(-2)));
                client.close();
                return;
            }
        }
        out.writeObject(new Message("server", Integer.toString(signal)));
        userType = (signal == 1) ? User.Type.CLIENT : User.Type.AGENT;
        loginUser.setOut(out);
        loginUser.setSocket(client);
        users.add(loginUser);
        availToChat.add(loginUser);
        break;
    } else {
        out.writeObject(new Message("server", Integer.toString(signal)));
    }
}

客户端部分

int receivedSignal;
loginUser = new User();
while (true) {
    new LoginForm();
    out.writeObject(loginUser);
    in = new ObjectInputStream(socket.getInputStream());
    message = (Message) in.readObject();
    receivedSignal = Integer.parseInt(message.getMsg());
    if (receivedSignal > 0) {
        break;
    } else {
        switch (receivedSignal) {
        case 0:
            JOptionPane.showMessageDialog(null, "User not found! Please try again.", "Login Error", JOptionPane.ERROR_MESSAGE);
            break;
        case -1:
            JOptionPane.showMessageDialog(null, "Invalid password! Please try again.", "Login Error", JOptionPane.ERROR_MESSAGE);
            break;
        case -2:
            JOptionPane.showMessageDialog(null, "User already logged in! Please log out first.", "Login Error", JOptionPane.ERROR_MESSAGE);
            break;
        }
    }
}

按钮事件监听器

loginButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            loginUser.setId(usernameTextField.getText());
            loginUser.setPw(new String(passwordPasswordField.getPassword()));
            dialog.dispose();
        }
    });

当我将Login表单放入其自己的类时,这是我以前的按钮监听器代码。我甚至得到" java.io.StreamCorruptedException:无效的流标题:"。它或多或少都是一样的。

public void actionPerformed(ActionEvent e) {
    loginUser.setId(usernameTextField.getText());
    loginUser.setPw(new String(passwordPasswordField.getPassword()));
    try {
        out.writeObject(loginUser);
        out.flush();
        in = new ObjectInputStream(socket.getInputStream());
        message = (Message) in.readObject();
        int receivedSignal = Integer.parseInt(message.getMsg());
        if (receivedSignal > 0) {
            JOptionPane.showMessageDialog(null, "Login success!", "Information", JOptionPane.INFORMATION_MESSAGE);
            frame.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "Login failed! Please try again.", "Information", JOptionPane.ERROR_MESSAGE);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Exception occurred: " + ex, "Error", JOptionPane.ERROR_MESSAGE);
    }
}
});

我知道这有点长,我非常感谢任何花时间的人。谢谢!

0 个答案:

没有答案