检查元素是否在客户端和服务器之间的散列映射中

时间:2015-02-26 02:09:55

标签: 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();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

使用NoSuchElementException,表示您的客户端失败,更准确,无论该名称是否在哈希映射中。

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

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

答案 1 :(得分:0)

在服务器中,您可以设置变量&#39; name&#39;到比利&#39; - 但是从不重新分配它,这意味着&#34; map.get(name)&#34;将永远返回相同的东西(具体来说,比利的密码)。

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

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

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

因此,如果在服务器中设置变量&#39; name&#39;和密码&#39;对于来自用户的输入值,您的语句应该是(忽略当前的空值,空格等):

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