我正在发送用户凭据以验证服务器,服务器应该响应此用户凭据是否有效。服务器可以获取客户端的请求。但客户无法得到任何回复。
关于服务器端的所有代码:
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;
}
}
}
...