java - 检查某个元素是否在客户端和服务器之间的 HashMap 中

标签 java hashmap client server

我正在使用 Java 尝试在服务器端检查用户发送的内容是否在 HashMap 中,但我的示例将以我给出的名称运行,无论它是否在 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();
        }
    }
}

最佳答案

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

更准确的说法是,无论名称是否在 HashMap 中,您的客户端都会失败,并出现 NoSuchElementException

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

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

关于java - 检查某个元素是否在客户端和服务器之间的 HashMap 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28733291/

相关文章:

c# - Swagger-codegen c# : Missing client member of type byte[]

java - 在 NetBeans 上运行用 Java 编写的客户端-服务器程序

java - 用于数据库规范化的准确表格

java - 为什么我的文本到图像方法返回全黑图像?

java - Liquibase 找不到变更日志文件

java - HashMap 中一个类的多个对象

java - 从对象获取值

java - 如何使用Java洗牌(尝试使用hashmap,不起作用)?

javascript - 如何使用JAX-RS在REST中将Javascript日期传递给Java LocalDate和LocalDateTime

angular - 在 Windows 操作系统上通过 npm 安装 swagger-codegen