检查元素是否在客户端和服务器之间的哈希图中

Checking if an element is in a hashmap between client and server

我正在使用 Java 尝试在我的服务器端检查用户发送的内容是否在 hashmap 中,但我的示例将 运行 的名称为 I'不管它是否在 hashmap 中,我们都给出了。

有人知道为什么会这样吗?

客户

package examPrep;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client 
{
    public static String name;
    public static void main(String[] args) throws Exception
    {
        //Client looks for the IP address of the server it wants
        InetAddress inet = InetAddress.getByName("localhost");

        //Creates a new socket connection on port 2021
        Socket s = new Socket(inet, 2021);

        ///Associates a scanner with the Input stream
        InputStream in = s.getInputStream();

        //Creates a scanner that looks for input, returns false if there is no more
        Scanner scanner = new Scanner(in);

        //Associates a PrintWriter to the OutputStream
        OutputStream o = s.getOutputStream();
        PrintWriter p = new PrintWriter(o);

        Scanner userInput = new Scanner(System.in);

        //Using system input to put data across
        System.out.println("Enter a username");
        name = userInput.nextLine();
        System.out.println("You typed in: " + name);

        p.println(name);

        //flush forces data to be sent even if the buffer is not full
        p.flush();

        //Reads the response and finishes
        String inputLine = scanner.nextLine();
        System.out.println("Client: " + inputLine);
    }
}

服务器

package examPrep;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Scanner;

public class Server 
{
    public static void main(String[] args) throws Exception
    {
        HashMap<String, String> map = new HashMap<String, String>();

        String name = "Billy";
        String password = "1234";
        map.put(name, password);

        //creates a new socket
        Socket s;

        //Socket is listening on port 2000
        ServerSocket ss = new ServerSocket(2021);

        //Constantly checking the connection
        while (true)
        {
            System.out.println("Server: waiting for connection ..");

            //Ready to accept a connection
            s = ss.accept();

            //Associates a scanner with the Input stream
            InputStream in = s.getInputStream();
            Scanner r = new Scanner(in);

            //Associates a PrintWriter to the OutputStream
            OutputStream o = s.getOutputStream();
            PrintWriter p = new PrintWriter(o);

            //Writes to the socket
            String inputLine;
            inputLine = r.nextLine();

            //This will always send the string back to the client
            //regardless of whether its in the hashmap or not
            if(inputLine == map.get(name))
            {
                // sends the String back to the client
                p.println("Hello " + inputLine);
            }
            else
                System.out.println("User " + inputLine + " not authorized");

            //Connection is closed with the client when finished
            p.close();
        }
    }
}

在服务器中,您将变量 'name' 设置为 'Billy' - 但之后永远不会重新分配它,这意味着 "map.get(name)" 将始终 return 相同的东西(具体来说,是 Billy 的密码)。

此外,在服务器中,您的行

if(inputLine == map.get(name))

正在比较字符串值 - 这意味着您应该使用等号而不是实例比较。当然,还有另一个问题:你的 hashmap 存储了用户名和密码——但你的客户端只要求输入用户名(而不是密码)。

因此,如果在服务器中将变量 'name' 和 'password' 设置为用户的输入值,那么您的语句应该是(暂时忽略空值、空格等):

if(password.equals(map.get(name))) {

难以置信:实际上不可能。

更准确的说法是您的客户端 失败, 带有 NoSuchElementException,,无论该名称是否在哈希图中。

原因是完全混淆了您的应用程序协议是什么。客户端正在发送它认为是名称的内容,但服务器正在根据固定名称的密码检查它。您的服务器代码应该使用 map.containsKey(inputLine) 来查找名称,而不是 Map.get(name).

在您尚未编码的后续交换中,客户端将发送密码,服务器应使用 map.get() 检索密码,并且应使用 .equals() 来比较字符串,而不是 ==.