2014-07-09 19 views
0

我正在編寫一個程序,我想要做一個簡單的客戶端服務器應用程序,它允許我從客戶端發送消息[list,add new user,delete a user] 。在刪除和添加一個用戶時,服務器有一個向量,通過它查找是否有某個用戶。 我引入值來檢查有匹配的值時的情況,但if doesn; t工作。任何ideeas? 和代碼是:服務器/客戶端問題:本地向量沒有通過右側迭代

客戶

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

}

和服務器:

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

回答

1

這兩句話

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

在很大程度上依賴於正確的equals方法的存在。請閱讀overriding equals and hashCode

所以你應該(或更好:必須)擴展類User如下:

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

(注:我創建了這些方法與Eclipse)

+0

非常感謝你,我從來沒有想過這個,再次感謝你 – tudoricc

0

user.get(i).equals(a)測試犯規知道如何檢查如果兩個用戶對象是相等的。爲User對象編寫自定義比較器,或者只是使用重寫的toString()方法來檢查是否相等。

if(user.get(i.toString()).equals(a.toString()))