Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TCP客户端/服务器和循环问题_Java_Loops_Client_Boolean - Fatal编程技术网

Java TCP客户端/服务器和循环问题

Java TCP客户端/服务器和循环问题,java,loops,client,boolean,Java,Loops,Client,Boolean,这是我第一次制作TCP客户机/服务器程序。我应该制作一个简单的Java客户端和服务器程序,向客户端请求用户名和密码。从那里,服务器检查其用户数据库(我试图使用mollow维度数组创建该数据库),查看是否存在匹配项,并向客户端返回一条消息“Hello”(用户名),然后告诉用户服务器上的所有其他注册用户 然而,我的具体问题是,每当我运行这两个时,当我到达服务器应该通过循环的部分时,所有显示的都是“当前在服务器上注册的其他用户包括:MATTCARLJOHN当前在服务器上注册的其他用户包括:MATTCA

这是我第一次制作TCP客户机/服务器程序。我应该制作一个简单的Java客户端和服务器程序,向客户端请求用户名和密码。从那里,服务器检查其用户数据库(我试图使用mollow维度数组创建该数据库),查看是否存在匹配项,并向客户端返回一条消息“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.");
                }
            }


        }
    }
}
import java.io.*;
导入java.net。*;
类TCPServer2
{
公共静态void main(字符串argv[])引发异常
{
ServerSocket welcomeSocket=新的ServerSocket(6789);
字符串[][]登录={{“MATT”,“UNCP”},{“JOHN”,“UNCP”},{“CARL”,“UNCP”};
字符串用户名;
字符串username1;
字符串密码;
字符串密码1;
while(true)
{
套接字连接Socket=welcomeSocket.accept();
BufferedReader inFromClient=
新的BufferedReader(新的InputStreamReader(connectionSocket.getInputStream());
DataOutputStream outToClient=新的DataOutputStream(connectionSocket.getOutputStream());
username=infomclient.readLine();
System.out.println(“收到的用户名:“+Username”);
password=inFromClient.readLine();
System.out.println(“收到密码:“+密码”);
username1=username.toUpperCase()+'\n';
password1=password.toUpperCase()+'\n';

对于(int i=0;i而言,当我运行代码时,它实际上似乎对我有效。在客户端上,请参见:

$ java TCPClient -classpath .
Username:
blah
Password:
blah
在服务器上,我看到:

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

您正在向
username1
password1
添加换行符。这些换行符不存在于硬编码登录凭据(
“MATT”、“UNCP”
)中,因此比较

if(Login[i][0].equals(username1) && Login[i][1].equals(password1)){
永远不会成功

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

哦,别忘了你所有邮件的换行符,f.e.:

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

是的,这对我来说也是所有的工作,这是它下面的大for循环,尽管这是我的主要问题,但是,让它识别一个成功的用户登录。