0

I am writing a programme where I want to do a simple client server application that allows me to send messages from the client[list ,add new user,delete a user].Both at deleting and adding a user the server has a vector where it looks through to find if there is that certain user. I introduce values to check the case when there are matching values but the if doesn;t work. Any ideeas? ANd the code is:

The client

public class Client {
  public static void main(String[] args) {

    Socket clientSocket = null;
    DataInputStream is = null;
    PrintStream os = null;
    DataInputStream inputLine = null;


    try {
        clientSocket = new Socket("localhost", 2227);
        os = new PrintStream(clientSocket.getOutputStream());
        is = new DataInputStream(clientSocket.getInputStream());
        inputLine = new DataInputStream(new BufferedInputStream(System.in));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host");
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to host");
    }

    if (clientSocket != null && os != null && is != null) {
        try {


            System.out.println("The client started. Type any text. To quit it type 'Ok'.");
            String responseLine;
            os.println(inputLine.readLine());
            while ((responseLine = is.readLine()) != null) {
                System.out.println(responseLine);
                if (responseLine.indexOf("Ok") != -1) {
                    break;
                }
                os.println(inputLine.readLine());
            }

            /*
             * Close the output stream, close the input stream, close the socket.
             */
            os.close();
            is.close();
            clientSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
    }
}

}

And the Server:

public class Server {
 public static void main(String args[]) {
    class User{
        String nume, parola,email;
        public User(String numi ,String pari , String emai){
            this.nume=numi;
            this.parola = pari;
            this.email = emai;
        }
        public String toString(){
            return this.nume +" "+ this.email + " "+this.email+" ";
        }
    }

    ServerSocket echoServer = null;
    String line;
    DataInputStream is;
    PrintStream os;
    Socket clientSocket = null;


    try {
        echoServer = new ServerSocket(2227);
    } catch (IOException e) {
        System.out.println(e);
    }

    System.out.println("The server started. To stop it press <CTRL><C>.");
    try {
        clientSocket = echoServer.accept();
        is = new DataInputStream(clientSocket.getInputStream());
        os = new PrintStream(clientSocket.getOutputStream());
        ArrayList<User> user = new ArrayList<User>();


        while (true) {
            line = is.readLine();
            if(line.startsWith("connect")){
                String[] words = line.split(" ");
                String nume = words[1];
                String parola = words[2];
                String email = words[3];
                User a = new User(nume,parola,email);
                boolean isThere = false;
                for (int i = 0 ; i< user.size();i++){
                    if (user.get(i).equals(a)){
                        os.println("The user exists");
                        isThere= true;
                        os.flush();
                    }
                }
                if (isThere==false){
                    os.println("He doesn;t exist");
                    user.add(a);
                    os.flush();
                    //os.println(user.toString());
                }
            }
            else if (line.contains("list")){
                os.println(user.toString());
            }
            else if (line.startsWith("delete")){
                String []userInfo = line.split(" ");
                String nume = userInfo[1];
                String parola = userInfo[2];
                String email = userInfo[3];
                user.remove(new User(nume,parola,email));

                os.println("I deleted him");
                os.flush();
            }
            //  os.println("From server: " + line);
        }
    } catch (IOException e) {
        System.out.println(e);
    }
  }
}
tudoricc
  • 709
  • 1
  • 12
  • 31

2 Answers2

1

These two statements

user.get(i).equals(a)
user.remove(new User(nume,parola,email))

heavily rely on the existence of a proper equals method. Read on overriding equals and hashCode.

So you should (or better: must) extend the class User as following:

class User{
    String nume, parola,email;
    public User(String numi ,String pari , String emai){
        this.nume=numi;
        this.parola = pari;
        this.email = emai;
    }
    public String toString(){
        return this.nume +" "+ this.email + " "+this.email+" ";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.nume == null) ? 0 : this.nume.hashCode());
        result = prime * result + ((this.parola == null) ? 0 : this.parola.hashCode());
        result = prime * result + ((this.email == null) ? 0 : this.email.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (this.getClass() != obj.getClass())
            return false;

        User other = (User) obj;

        if (this.nume == null) {
            if (other.nume != null)
                return false;
        } else if (!this.nume.equals(other.nume))
            return false;

        if (this.parola == null) {
            if (other.parola != null)
                return false;
        } else if (!this.parola.equals(other.parola))
            return false;

        if (this.email == null) {
            if (other.email != null)
                return false;
        } else if (!this.email.equals(other.email))
            return false;

        return true;
    }
}

(Note: I created these methods with Eclipse.)

Community
  • 1
  • 1
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0

Your user.get(i).equals(a) test doesnt know how to check if two User Objects are equal. Write a custom comparator for User objects or just use your overridden toString() method to check for equality.

   if(user.get(i.toString()).equals(a.toString()))
Revive
  • 2,248
  • 1
  • 16
  • 23