【问题标题】:Java TCP Clients/Servers and loop issuesJava TCP 客户端/服务器和循环问题
【发布时间】:2012-05-13 09:09:16
【问题描述】:

这是我第一次制作TCP Client/Server 程序。我应该制作一个简单的 Java 客户端和服务器程序,向客户端询问用户名和密码。服务器从那里检查其用户数据库(我尝试使用覆盖维数组创建)以查看是否有匹配项并向客户端返回一条消息“你好”(用户名),然后告诉用户所有其他服务器上的注册用户。

我的具体问题然而,每当我运行这两个时,当我到达服务器应该通过循环的部分时,显示的只是“其他目前在服务器上注册的用户包括:MATTCARLJOHN 目前在服务器上注册的其他用户包括:MATTCARLJOHN 目前在服务器上注册的其他用户包括:MATTCARLJOHN"

似乎我的服务器完全忽略了我的 for 循环和 if 语句,它应该检查身份验证并直接跳到该循环,无论我在客户端输入什么用户名和密码。

以下是两个程序: 客户

import java.io.*;
import java.net.*;

class TCPClient2
{
    public static void main(String argv[]) throws Exception
    {
         String userName;
         String passWord;
         String loginInfo;
         String loginInfo2;

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



         clientSocket.close();
     }
}

和服务器

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 username;
        String username1;
        String password;
        String password1;
        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("Username received: " + username);
            password = inFromClient.readLine();
            System.out.println("Password received: " + password);
            username1=username.toUpperCase() + '\n';
            password1=password.toUpperCase() + '\n';

            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("Other users registered on the server currently include: ");
                    for(int k = 0; k<Login.length; k++){
                        outToClient.writeBytes(Login[k][0]);}
                else
                    outToClient.writeBytes("Invalid Username and/or password.");
                }
            }


        }
    }
}

任何帮助将不胜感激,这是我第一次在网站上发帖,我对网络和 Java 非常陌生,所以如果我的问题看起来令人困惑,请原谅我。

谢谢。

【问题讨论】:

    标签: java loops client boolean


    【解决方案1】:

    您正在向username1password1 添加换行符。这些换行符不存在于硬编码的登录凭据 ("MATT","UNCP") 中,因此比较...

    if(Login[i][0].equals(username1) && Login[i][1].equals(password1)){
    

    永远不会成功。

    只需省略 username1password1 中的换行符(至少在 if 之后)。

    哦,不要忘记所有消息的换行符,f.e.:

    outToClient.writeBytes("Invalid Username and/or password.\n");
    //                                                       /
    //                                                 added the \n
    

    【讨论】:

      【解决方案2】:

      当我运行它时,代码实际上似乎对我有用。在客户端上看到:

      $ java TCPClient -classpath .
      Username:
      blah
      Password:
      blah
      

      在我看到的服务器上:

      $ java TCPServer -classpath .
      Username received: blah
      Password received: blah
      

      【讨论】:

      • 是的,这对我也有用,虽然这是我的主要问题,但它是它下面的大 for 循环,并让它识别成功的用户登录。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2019-02-26
      • 2012-11-03
      • 1970-01-01
      相关资源
      最近更新 更多