2012-05-09 111 views
1

我正在尝试在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(); 
} 
} 

这是我第一次问计算器上的一个问题,但我已经浏览了很多过去。所以我只想说,感谢你为你提供的伟大社区。

+0

另外,我只是想说明。我知道这是存储密码的不好方法。此应用程序用于教育目的,因此不需要加密/散列。 – Catch22

+2

欢迎!在查看代码之前 - 您是否尝试使用内置于IDE中的调试器进行调试? – home

+1

我建议你沟二维数组,而不是使用'HashMap'。 HashMaps允许您存储键值对。一旦获得登录名称,您只需获取与该密钥(登录名称)对应的值(密码)即可。您目前正在执行此操作的方式很可能会根据用户提供的用户名打印“无效凭证...”一次或两次。 – npinti

回答

0

我不知道这是否会帮助你,但检查你是否不必在每次writeBytes调用后刷新以真正输出数据到客户端。

尝试添加outToClient.flush();

+0

它似乎重复“无效的用户名和/或密码”行,在我输入一个命令后,我尝试添加一个outToClient.flush();在该行之后,但它仍然发送两行。不要输入if(Command.equals(“..”))语句,因为要发送的行可能会与我的客户端发回线路,或者多次打印收到的线路。 – Catch22

0

这是我的意见的延续:通过像这样String[][] Login = {{"MATT","UNCP"},{"JOHN","UNCP"},{"CARL","UNCP"}};

这是我会怎么做,首先,我将取代这个

Map<String, String> userDetails = new HashMap<String, String>(); 
userDetails.put("MAT", "UNCP"); 
userDetails.put("JOHN","UNCP"); 
userDetails.put("CARL", "UNCP"); 

然后,而不是这样的:

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"); 
      } 
     } 

我这样做:

if (userDetails.get(username1).equals(password1)) 
{ 
    outToClient.writeBytes("Hello " + username1); 
       outToClient.writeBytes("\nOther users registered on the server currently include: \n"); 

    for (String str : userDetails.keySet() 
    { 
     outToClient.writeBytes(str); 
    } 

} 
else 
{ 
    outToClient.writeBytes("Invalid Username and/or password.\n"); 
} 

我看到它的方式,系统正在工作并打印Invalid credentials字符串是因为,假设您提供了这些日志详细信息:CARLUNCP,您传递的是第二组证书,因此您的循环将检查通过将它们与第一组进行比较并且由于它们不匹配,它将打印该消息。在第二次迭代中,它将看到凭证匹配并将登录您。

欲了解更多信息,请查看JavaDoc。除了通常的ListSet之外,HashMap是一个集合,通常是一件好事。

+0

谢谢,生病了试试看并返回报告,我认为这是正确的:; \t UserDetails.put(“JOHN”,“UNCP”)(“MATT” ,“UNCP”)(“..”,“...”); ??或将它; \t Map userDetails = new HashMap (); \t userDetails.put ( “约翰”, “UNCP” “MATT”, “UNCP”); – Catch22

+0

@ Catch22:我添加了更多细节。 – npinti

+0

谢谢,它现在似乎更好。虽然登录后会冻结,登录凭据无效时会断开连接。 – Catch22

相关问题