2

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

然而,我的具体问题是,每当我运行这两个时,当我到达服务器应该通过循环的部分时,显示的只是“当前在服务器上注册的其他用户包括: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 非常陌生,所以如果我的问题看起来令人困惑,请原谅我。

谢谢你。

4

2 回答 2

1

您将换行符附加到username1and password1。这些换行符不存在于硬编码的登录凭据 ( "MATT","UNCP") 中,因此比较...

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

永远不会成功。

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

哦,不要忘记所有消息的换行符,fe:

outToClient.writeBytes("Invalid Username and/or password.\n");
//                                                       /
//                                                 added the \n
于 2012-05-03T22:32:27.253 回答
0

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

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

在我看到的服务器上:

$ java TCPServer -classpath .
Username received: blah
Password received: blah
于 2012-05-03T20:53:58.957 回答