对象输入/输出流。如何重复和有效地发送更新的,相同的对象?

时间:2014-02-24 15:55:44

标签: java list networking arraylist io

尝试使用TCP发送数据列表时遇到了一些麻烦。我已经制作了一个较小版本的问题,并夸大了一点。

客户端

public class Client {

    DListStorage dl = new DListStorage();
    Data d = new Data();

    Socket socket;

    public Client(){
        d.x = 0;
        dl.dl.add(d);

        try {
            socket = new Socket(InetAddress.getLocalHost(), port);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

            for(int i = 0; i < 3; i++){
                oos.writeObject(dl);
                dl.dl.get(0).x++;
                System.out.println("Client: " + dl.dl.get(0).x);
            }

            socket.close();
            socket = new Socket(InetAddress.getLocalHost(), port);
            oos = new ObjectOutputStream(socket.getOutputStream());

            for(int i = 0; i < 3; i++){
                oos.writeObject(dl);
                dl.dl.get(0).x++;
                System.out.println("Client: " + dl.dl.get(0).x);
            }
        } catch (IOException e) {
                e.printStackTrace();
        }
    }
}

SERVER

public class NewConnections {

    ServerSocket ssocket;
    Socket socket;

    public NewConnections(){
        acceptConnections.start();
    }

    Thread acceptConnections = new Thread(new Runnable(){
        public void run(){
            try {
                ssocket = new ServerSocket(port, 0, InetAddress.getLocalHost());
                socket = ssocket.accept();
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

                DListStorage dl;

                for(int i = 0; i < 3; i++){
                    dl = (DListStorage)ois.readObject();
                    System.out.println("Server: " + dl.dl.get(0).x);
                }

                socket.close();
                socket = ssocket.accept();
                ois = new ObjectInputStream(socket.getInputStream());

                for(int i = 0; i < 3; i++){
                    dl = (DListStorage)ois.readObject();
                    System.out.println("Server: " + dl.dl.get(0).x);
                }

                ssocket.close();
            } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
            }
        }
    });
}

我的结果如下。

Client: 0.0
Client: 1.0
Server: 0.0
Client: 2.0
Server: 0.0
Server: 0.0
Client: 3.0
Client: 4.0
Server: 3.0
Client: 5.0
Server: 3.0
Server: 3.0

Data和DataStorage都实现了Serializable,DataStorage包含 public List<Data> dl = new ArrayList<>(); 我无法弄清楚为什么服务器没有正确读取浮点数x。仅在初次阅读时。

由于

1 个答案:

答案 0 :(得分:0)

尝试:

服务器:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class NewConnections {

public NewConnections() {
    acceptConnections.start();
}

Thread acceptConnections = new Thread(new Runnable() {
    public void run() {
        try {
            int port = 81;
            ServerSocket ssocket = new ServerSocket(port, 0, InetAddress.getLocalHost());
            while (true) {
                System.out.println("waiting...");
                Socket socket = ssocket.accept();
                // buffered reader to read a line at a time from client
                BufferedReader ois = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));

                String x = null;
                // read till input is available from client
                while ((x = ois.readLine()) != null) {
                    System.out.println("Server: " + x);
                }
                ois.close();
                socket.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}

客户端:

import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Client {

public Client() {
    int port = 81;
    int x = 0;
    try {

        Socket socket = new Socket(InetAddress.getLocalHost(), port);
        // use auto flush to send the data immediately to the server
        PrintWriter oos = new PrintWriter(socket.getOutputStream(), true);

        for (int i = 0; i < 3; i++) {
            x++;
            oos.println(x);
            System.out.println("Client: " + x);
        }
        oos.close();
        socket.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}