UDP ArrayList <object> Check IfClientExists
<p>I've made a simple UDP chat server and client</p>

<p>Client sends a packet containing a string, server receives it.</p>

<p>Upon receipt, the server should be able to tell if the IP AND Port number are in a list of Clients.</p>

<p>I made an object class called ClientDetails, </p>

<pre><code>class ClientDetails {
   int port; 
   String name;
   InetAddress address;

    ClientDetails(InetAddress address, int port, String name) {
        this.address = address;
        this.port = port;
        this.name = name;
    }
</code></pre>

<p>and in the Server I create an ArrayList of that object to hold the details of active clients.</p>

<p>I have constructed some If statements and corresponding methods to perform most of the operations, but the crux is finding out whether the user is in the list or not on every packet received.</p>

<p>I want to use something like </p>

<pre><code>if (!IsCurrentlyAUser){
then addNewUserToList();
else checkUserNameValid//ensures the String sent is not one of the keywords(who, whoami, etc)
 addNewUser();}
</code></pre>

<p>The idea is that IsCurrentlyAUser checks a method CheckClientExists,</p>

<pre><code>private boolean checkClientExists(InetAddress ip, int port, String name){


    if (!clientList.contains(ip) &amp;&amp; !clientList.contains(port)){
    return false;
}
     return true;

    }
</code></pre>

<p>Clearly this won't work, but I don't know how to make it or come up with something more viable.</p>

<p>EDIT: To clarify the problem and possibly complicate things:</p>

<p>The check is based on a clients IP AND Port. In that instance, only the ip and port are checked among the records. If they exist, the client continues, if they don't, they get added, wherein the name entered will be checked.</p>

<p>EDIT:</p>

<p>Updated ClientDetails with the following</p>

<pre><code>@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + port;
return result;
}





@Override
public boolean equals(Object obj) {
if (this == obj)
    return true;
if (obj == null)
    return false;
if (getClass() != obj.getClass())
    return false;
ClientDetails other = (ClientDetails) obj;
if (address == null) {
    if (other.address != null)
        return false;
} else if (!address.equals(other.address))
    return false;
if (name == null) {
    if (other.name != null)
        return false;
} else if (!name.equals(other.name))
    return false;
if (port != other.port)
    return false;
return true;

}
</code></pre>

<p>Running the following in the Main Server</p>

<pre><code>    if(request.equals("who") ||request.equals("whoami")||
        request.equals("all")||request.equals("bye")){
         //do something 
          System.out.println("User Entered Invalid Name ");
         }else{
              System.out.println("User Entered a Valid Name ");
               clientName =request;
              clients.add(new ClientDetails(clientIP,clientPort,clientName));
                    }
             System.out.println("set contains " +clients.toString());
</code></pre>

<p>seems to not prevent duplicates, which suggests that the hash/equals is wrong. This was generated by eclipse, which is the best I could do. </p>
