套接字Java之间共享的ArrayList

时间:2018-07-07 12:42:01

标签: java sockets arraylist objectinputstream objectoutputstream

我有2个套接字:一个客户端套接字(客户端类)和一个并发服务器套接字(ServerC类),它们可以同时处理更多客户端。 线程服务器在连接时将数组发送到客户端。 如果我在线程服务器内部更新了阵列,则程序可以正常工作,并且下一个客户端在连接时会收到更新的阵列。 当客户端更新接收到的阵列时,问题就开始了,但是如果新客户端连接到服务器,则该阵列不是“新”阵列。这意味着不共享对象。 正常吗有可能解决这个问题吗? 谢谢你,我的英语很抱歉。

public class Person implements Serializable {   
    private String name;
    private int age;

    public Person(String name, int age){
        this.name=name;
        this.age=age;
    }

    public void setNome (String name){
        this.name = name;
    }

    @Override
    public String toString(){
        return name + " " + age;
    }
}

public class Client {
    public static void main(String[] args) {
        try {
            Socket socketClient = new Socket("127.0.0.1",1500);
            ObjectOutputStream outToServer = new ObjectOutputStream(socketClient.getOutputStream());
            ObjectInputStream inFromServer = new ObjectInputStream(socketClient.getInputStream());
            ArrayList<Person> people = (ArrayList<Person>)inFromServer.readObject();
            for (Person p : people) {
                    System.out.println(p); //The second client will print Kevin too
            }
            people.add(new Person("John",54)); //This person won't be never printed for new clients
            socketClient.close();
            outToServer.close();
            inFromServer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }   
     }
}

public class ServerC {
    public static void main(String[] args) {
        ArrayList<Person> people = new ArrayList<Person>();
        people.add(new Person("Alex",21));
        ServerSocket socketServer = null;
        try {
                socketServer = new ServerSocket(1500);
        } catch (IOException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        }
        int id=0;
        while (true) {
            try {
                Socket socket = socketServer.accept();
                id = id + 1;
                ClientServiceThread cst = new ClientServiceThread(socket,id, people);
                cst.start();
            } catch (IOException e) {
                break;
            }
        }
    }
}

class ClientServiceThread extends Thread {
    private Socket socket;
    private int id=-1;
    private ArrayList<Person> people;

    public ClientServiceThread (Socket socket, int id, ArrayList<Person> people) {
            this.socket = socket;
            this.id=id;
            this.people = people;
    }

    public void run() {                 
        System.out.println(people);
        try {
            ObjectOutputStream outToClient = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream inFromClient = new ObjectInputStream(socket.getInputStream());                                                                                  

            outToClient.writeObject(people);
            people.add(new Person("Kevin",7));                            

            outToClient.close();
            inFromClient.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案