Check if an item is in a hashmap between the client and the server

advertisements

I am using Java to try make a check on my server side on whether or not what the user sends over is in the hashmap, but my example will run with the name I've given regardless of whether its in the hashmap or not.

Anyone know why this is happening?

client

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);
    }
}

server

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


Hard to believe: impossible actually.

It would be more accurate to say that your client fails, with a NoSuchElementException, regardless of whether the name is in the hashmap or not.

The reason for that is a total confusion about what your application protocol is. The client is sending what it thinks is the name, but the server is checking it against a fixed name's password. Your server code should be using map.containsKey(inputLine) to lookup the name, not Map.get(name).

On a subsequent exchange which you haven't coded yet, the client would send the password, and the server should use map.get() to retrieve the password, and it should be using .equals() to compare strings, not ==.