ClientSideNewGuestHandler类以字符串的形式(在本例中为“ newProfileRequest”)向服务器发送特定的提示,以使其采取与用户要执行的操作相对应的步骤。然后,它根据服务器的响应做出反应,但是在这种情况下,客户端程序只是挂起,因为它没有收到来自服务器的任何信息,即使在服务器方面,返回字符串恰好在return语句之前。为什么会这样?
public class ClientSideNewGuestHandler {
public static void handler(String newGuestName, String newGuestSurname, String newGuestPhone, String newGuestEmail, String newGuestPassword, String newGuestPassword2) {
try {
Socket clientSocket = new Socket(NetworkConstants.HOST, NetworkConstants.PORT);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.println("newProfileRequest");
out.println(newGuestName);
out.println(newGuestSurname);
out.println(newGuestPhone);
out.println(newGuestEmail);
out.println(newGuestPassword);
out.println(newGuestPassword2);
String serverResponse = in.readLine();
System.out.println(serverResponse);
//TODO client not receiving response
if(serverResponse.equals("success")) {
AlertBox.display("Account Created", "Account created");
bookingClient.UserInterface.guestEmailField.setText(newGuestEmail);
bookingClient.UserInterface.window.setScene(bookingClient.UserInterface.guestLoginScene);
bookingClient.UserInterface.newGuestNameField.clear();
bookingClient.UserInterface.newGuestSurnameField.clear();
bookingClient.UserInterface.newGuestEmailField.clear();
bookingClient.UserInterface.newGuestPhoneField.clear();
bookingClient.UserInterface.newGuestPasswordField.clear();
bookingClient.UserInterface.newGuestPassword2Field.clear();
}else if(serverResponse.contains("error")){
AlertBox.display("Error", "Invalid Input(s); " + serverResponse.substring(5, serverResponse.length()));
}else {
switch(serverResponse) {
case "allEmpty": AlertBox.display("Error", "No entries");
break;
case "failure": AlertBox.display("Error", "Unexpected error. Contact system administrator");
}
}
clientSocket.close();
}catch(IOException e) {
AlertBox.display("Error", "No response from server");
}
}
}
服务器类:
public class SocketServer {
int portNumber = 44444;
ServerSocket serverSocket = null;
public void runServer() {
try {
serverSocket = new ServerSocket(portNumber);
}catch(IOException e) {
System.out.println(e.getMessage());
}
System.out.println("Server started. Waiting for Connections...");
while(true) {
try{
Socket clientSocket = serverSocket.accept();
new Thread(new SystemLogicRunnable(clientSocket)).start();
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
}
线程:
public class SystemLogicRunnable implements Runnable{
protected Socket clientSocket = null;
public SystemLogicRunnable(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
String clientRequest;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
clientRequest = in.readLine();
if(clientRequest.equals("loginRequest")) {
System.out.println("\nClient " + clientSocket.getInetAddress() + " has sent a login request over port #" + clientSocket.getLocalPort());
String email, password;
email = in.readLine();
System.out.println("Email recieved as: " + email);
password = in.readLine();
System.out.println("Password recieved as: " + password);
out.println(GuestLoginHandler.handler(email, password));
}else if(clientRequest.equals("newProfileRequest")) {
System.out.println("\nClient " + clientSocket.getInetAddress() + " has sent a new user request over port #" + clientSocket.getLocalPort());
String newGuestName, newGuestSurname, newGuestPhone, newGuestEmail, newGuestPassword, newGuestPassword2;
newGuestName = in.readLine();
System.out.println("Name recieved as: " + newGuestName);
newGuestSurname = in.readLine();
System.out.println("Surname recieved as: " + newGuestSurname);
newGuestPhone = in.readLine();
System.out.println("Phone recieved as: " + newGuestPhone);
newGuestEmail = in.readLine();
System.out.println("Email recieved as: " + newGuestEmail);
newGuestPassword = in.readLine();
System.out.println("Password recieved as: " + newGuestPassword);
newGuestPassword2 = in.readLine();
System.out.println("Password2 recieved as: " + newGuestPassword2);
out.print(NewGuestHandler.handler(newGuestName, newGuestSurname, newGuestPhone, newGuestEmail, newGuestPassword, newGuestPassword2));
}
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
服务器端访客登录逻辑:
public class NewGuestHandler extends gui.UserInterface{
private static ArrayList<Guest> gList = new ArrayList<Guest>();
public static String handler(String newGuestName, String newGuestSurname, String newGuestPhone, String newGuestEmail, String newGuestPassword, String newGuestPassword2) {
String[] fields = {newGuestName, newGuestSurname, newGuestPhone, newGuestEmail, newGuestPassword, newGuestPassword2};
Boolean allEmpty = true;
Boolean error = false;
String message = "";
for(String x : fields) {
if(!x.isEmpty()) {
allEmpty = false;
}
}
if(allEmpty) {
System.out.println("Request Denied [allEmpty]");
return "allEmpty";
}else {
if(newGuestName.isEmpty()){
message += "noName\n";
error = true;
}else if(!InputValidation.validateName(newGuestName)){
message += "badName\n";
error = true;
}
if(newGuestSurname.isEmpty()){
message += "noSurname\n";
error = true;
}else if(!InputValidation.validateSurname(newGuestSurname)){
message += "badSurname\n";
error = true;
}
if(newGuestPhone.isEmpty()){
message += "noPhone\n";
error = true;
}else if(!InputValidation.validatePhoneNo(newGuestPhone)){
message += "badPhone\n";
error = true;
}
if(newGuestEmail.isEmpty()){
message += "noEmail\n";
error = true;
}else if(InputValidation.validateEmailExists(newGuestEmail)){
message += "emailExists\n";
error = true;
}else if(!InputValidation.validateEmail(newGuestEmail)){
message += "badEmail\n";
error = true;
}
if(newGuestPassword.isEmpty()){
message += "noPass\n";
error = true;
}else if(!InputValidation.validatePassword(newGuestPassword)){
message += "badPass\n";
error = true;
}else if(!InputValidation.matchPassword(newGuestPassword, newGuestPassword2)){
message += "passMismatch";
error = true;
}
if(error) {
String serverOut = "Request Denied [";
if(message.contains("noName")) {serverOut += "noName, ";}
if(message.contains("badName")) {serverOut += "badName, ";}
if(message.contains("noSurname")) {serverOut += "noSurname, ";}
if(message.contains("badSurname")) {serverOut += "badSurname, ";}
if(message.contains("noPhone")) {serverOut += "noPhone, ";}
if(message.contains("badPhone")) {serverOut += "badPhone, ";}
if(message.contains("noEmail")) {serverOut += "noEmail, ";}
if(message.contains("emailMismatch")) {serverOut += "emailMismatch, ";}
if(message.contains("badEmail")) {serverOut += "badEmail, ";}
if(message.contains("noPass")) {serverOut += "noPass, ";}
if(message.contains("badPass")) {serverOut += "badPass, ";}
if(message.contains("passMismatch")) {serverOut += "passMismatch, ";}
serverOut += "]";
System.out.println(serverOut);
return "error " + serverOut;
}else if(!error){
FileManager.openGuestFile();
gList = FileManager.getGuestList();
String name = newGuestName.substring(0, 1).toUpperCase() + newGuestName.substring(1, newGuestName.length()).toLowerCase();
String surname = newGuestSurname.substring(0, 1).toUpperCase() + newGuestSurname.substring(1, newGuestSurname.length()).toLowerCase();
Guest newGuest = new Guest("GST-" + String.format("%06d", gList.size()), name, surname, newGuestPassword, newGuestEmail, newGuestPhone);
gList.add(newGuest);
FileManager.saveFile(Constants.GUEST_FILE, gList);
System.out.println("Request Granted");
return "success";
}
}
return "failure";
}
}