我正在嘗試在Java中編寫控制檯客戶機 - 服務器應用程序;使用套接字,我目前有一個簡單的登錄系統和一個簡單的命令系統。儘管登錄系統將「無效的用戶名和密碼」行打印到客戶端,但無論用戶輸入了正確的憑據,登錄系統似乎都能正常工作。 - 連接絕對有效。爲什麼我的應用程序不能輸入if語句
但是,命令系統似乎根本不起作用,當服務器接收到該命令時,它似乎不會發回任何內容。
所以我的主要問題是爲什麼我的服務器收到命令時沒有發回任何東西回客戶端?
這裏是我的服務器:
import java.io.*;
import java.net.*;
class TCPServer2
{
public static void main(String argv[]) throws Exception
{
ServerSocket welcomeSocket = new ServerSocket(6789);
String[][] Login = {{"MATT","UNCP"},{"JOHN","UNCP"},{"CARL","UNCP"}};
String Command;
String username;
String username1;
String password;
String password1;
String cmd;
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
username = inFromClient.readLine();
System.out.println("\nUsername received: " + username);
password = inFromClient.readLine();
System.out.println("\nPassword received: " + password);
username1=username.toUpperCase();
password1=password.toUpperCase();
for(int i = 0; i<Login.length; i++)
{
if(Login[i][0].equals(username1) && Login[i][1].equals(password1))
{
outToClient.writeBytes("Hello " + username1);
outToClient.writeBytes("\nOther users registered on the server currently include: \n");
for(int k = 0; k<Login.length; k++)
{
outToClient.writeBytes(Login[k][0]);
}
}
else {
outToClient.writeBytes("Invalid Username and/or password.\n");
}
}
BufferedReader inFromClient2 =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient2 = new DataOutputStream(connectionSocket.getOutputStream());
Command = inFromClient2.readLine();
System.out.println("\nCommand received: " + Command);
if(Command.equals("listTranslations"))
{
outToClient2.writeBytes("English,Thai,Geordie,etc.");
}
else
{
if(Command.equals("getCost"))
{
outToClient2.writeBytes("£100\n");
}
else
{
outToClient2.writeBytes("Invalid Command");
}
}
}
}
}
這裏是我的客戶:
import java.io.*;
import java.net.*;
class TCPClient2
{
public static void main(String argv[]) throws Exception
{
String userName;
String passWord;
String loginInfo;
String loginInfo2;
String loginInfo3;
String command;
String commandInfo;
String commandInfo2;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Username: ");
userName = inFromUser.readLine();
outToServer.writeBytes(userName + "\n");
System.out.println("Password: ");
passWord = inFromUser.readLine();
outToServer.writeBytes(passWord + "\n");
loginInfo = inFromServer.readLine();
System.out.println(loginInfo);
loginInfo2 = inFromServer.readLine();
System.out.println(loginInfo2);
loginInfo3 = inFromServer.readLine();
System.out.println(loginInfo3);
System.out.println("Please enter a command: ");
command = inFromUser.readLine();
outToServer.writeBytes(command);
commandInfo = inFromServer.readLine();
System.out.println(commandInfo);
commandInfo2 = inFromServer.readLine();
System.out.println(commandInfo);
clientSocket.close();
}
}
這是我第一次問計算器上的一個問題,但我已經瀏覽了很多過去。所以我只想說,感謝你爲你提供的偉大社區。
另外,我只是想說明。我知道這是存儲密碼的不好方法。此應用程序用於教育目的,因此不需要加密/散列。 – Catch22
歡迎!在查看代碼之前 - 您是否嘗試使用內置於IDE中的調試器進行調試? – home
我建議你溝二維數組,而不是使用'HashMap'。 HashMaps允許您存儲鍵值對。一旦獲得登錄名稱,您只需獲取與該密鑰(登錄名稱)對應的值(密碼)即可。您目前正在執行此操作的方式很可能會根據用戶提供的用戶名打印「無效憑證...」一次或兩次。 – npinti