java - 无法从服务器套接字获得响应

时间:2016-05-04 20:50:34

标签: java multithreading sockets inputstream outputstream

我正在发送用户凭据以验证服务器,服务器应该响应此用户凭据是否有效。服务器可以获取客户端的请求。但客户无法得到任何回复。

关于服务器端的所有代码:

ClientHandler.java

...

public ClientHandler(Socket socket) {
        try {
            client = socket;
            input = client.getInputStream();
            output = client.getOutputStream();
            inputHandler = new ClientInputHandler(input);
            outputHandler = new ServerOutputHandler(output);
        } catch (IOException ex) {
            Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 @Override
    public void run() {

        inputHandler.start();
       outputHandler.start();
        String clientMessage = null;
        while(clientMessage == null) {
           clientMessage  = inputHandler.getClientMessage();
        }         
        System.out.println(clientMessage);
        User u = gson.fromJson(clientMessage, User.class);
        boolean successful = ClientAuthentication.validateCredentials(u);
        if(successful) {
            outputHandler.setServerMessage("1");
        }
        else {
            outputHandler.setServerMessage("0");
        } 


    }
...

ClientInputHandler.java

...
@Override
    public void run() {
        try {
            while (true) {
                while ((clientMessage = bufferedReader.readLine()) != null) {
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(ClientInputHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
...

ServerOutputHandler.java

...
    @Override
    public void run() {
        while (true) {
            if(serverMessage != null) {
                 printWriter.println(serverMessage);
                 serverMessage = null;
            }          
        }
    }
...

0 个答案:

没有答案