服务器/客户端问题:本地向量未通过右侧迭代

时间:2014-07-09 10:57:22

标签: java client-server

我正在编写一个程序,我想做一个简单的客户端服务器应用程序,允许我从客户端发送消息[列表,添加新用户,删除用户]。删除和添加用户时,服务器有一个它通过向量查找是否有某个用户。 我介绍了值来检查具有匹配值的情况,但if不做任务。任何想法? 代码是:

客户

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

2 个答案:

答案 0 :(得分: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创建了这些方法。)

答案 1 :(得分:0)

您的user.get(i).equals(a)测试不知道如何检查两个用户对象是否相等。为User对象编写自定义比较器,或者只使用重写的toString()方法来检查是否相等。

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