从套接字流中读取对象没有得到正确的值

Reading Object from Socket Stream Isn't Getting Correct Values

所以我正在尝试为 class 做这个项目,我在其中模拟使用套接字的分布式计算机网络。然后每台计算机都会写入一个包含其ID和时间戳的事件对象,每台计算机也会读取传入的事件对象并将自己的时间戳与发送的时间戳进行比较。

我原以为一切正常,直到我查看我的日志,发现有些东西似乎很奇怪运行。我注意到计算机正在一遍又一遍地读取同一个对象,即使它应该接收到其他计算机生成的新事件。

这是生成事件并将事件写入套接字的代码

private void generateEvents(Random rand, int numOfEvents) throws IOException {
        int temp;
        while (eventCount < numOfEvents) {
            int choice = rand.nextInt(5);
            synchronized (timestamp) {
                if (choice == 0) {
                    temp = timestamp.get(identifier);
                    ++temp;
                    timestamp.set(identifier, temp);
                    log.write(eventCount + ": " + "Sent to myself. " + timestamp.toString());
                } else {
                    int randC = rand.nextInt(outputClients.size());
                    ClientSocket cc = outputClients.get(randC);
                    Event sentEvent = new Event(identifier, timestamp);
                    log.write(sentEvent.from + "  " + sentEvent.timestamp);
                    cc.out.writeObject(sentEvent);
                    log.write(eventCount + ": " + "Sent to " + randC + ": " + timestamp.toString());
                }
            }
            eventCount++;
            // Add sleep for breathing room
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

此代码由主计算机线程 运行。

下面是读取对象的代码

while (isAlive) {
                    Event event;
                    try {
                        event = (Event) in.readObject();
                        if (event != null) {
                            log.write("Reading from " + event.from + ": " + event.timestamp.toString());
                            log.write("Old timestamp: " + timestamp.toString());
                            synchronized (timestamp) {
                                int temp;
                                for (int i = 0; i < timestamp.size(); ++i) {
                                    if (event.getTimestamp().get(i) > timestamp.get(i)) {
                                        timestamp.set(i, event.getTimestamp().get(i));
                                    }
                                }
                                temp = timestamp.get(event.getFromID());
                                ++temp;
                                timestamp.set(event.getFromID(), temp);
                            }
                            log.write("Updated timestamp: " + timestamp.toString());
                        }
                    } catch (ClassNotFoundException e) {
                        // Do nothing
                    } catch (EOFException e) {
                        // Do nothing
                    }

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

此代码 运行 由计算机 class 创建的单独线程class。

你可以在这里查看我的完整代码:https://github.com/nollidnosnhoj/time-table-exchange

所以当我 运行 程序时,我开始看到方便的模式:

Computer is starting!
[0, 0, 0, 0, 0]
Connecting to other computers in network
Connected to computers. Start reading and writing events
0  [0, 0, 0, 0, 0]
0: Sent to 2: [0, 0, 0, 0, 0]
0  [0, 0, 0, 0, 0]
1: Sent to 2: [0, 0, 0, 0, 0]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [0, 0, 0, 0, 0]
Updated timestamp: [0, 0, 0, 0, 1]
0  [0, 0, 0, 0, 1]
2: Sent to 2: [0, 0, 0, 0, 1]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [0, 0, 0, 0, 1]
Updated timestamp: [0, 1, 1, 0, 1]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [0, 1, 1, 0, 1]
Updated timestamp: [1, 1, 1, 3, 1]
0  [1, 1, 1, 3, 1]
3: Sent to 1: [1, 1, 1, 3, 1]
0  [1, 1, 1, 3, 1]
4: Sent to 2: [1, 1, 1, 3, 1]
0  [1, 1, 1, 3, 1]
5: Sent to 3: [1, 1, 1, 3, 1]
0  [1, 1, 1, 3, 1]
6: Sent to 3: [1, 1, 1, 3, 1]
0  [1, 1, 1, 3, 1]
7: Sent to 1: [1, 1, 1, 3, 1]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [1, 1, 1, 3, 1]
Updated timestamp: [1, 1, 1, 3, 2]
0  [1, 1, 1, 3, 2]
8: Sent to 2: [1, 1, 1, 3, 2]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [1, 1, 1, 3, 2]
Updated timestamp: [1, 2, 1, 3, 2]
0  [1, 2, 1, 3, 2]
9: Sent to 1: [1, 2, 1, 3, 2]
10: Sent to myself. [2, 2, 1, 3, 2]
11: Sent to myself. [3, 2, 1, 3, 2]
0  [3, 2, 1, 3, 2]
12: Sent to 2: [3, 2, 1, 3, 2]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [3, 2, 1, 3, 2]
Updated timestamp: [3, 4, 4, 5, 2]
0  [3, 4, 4, 5, 2]
13: Sent to 2: [3, 4, 4, 5, 2]
14: Sent to myself. [4, 4, 4, 5, 2]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [4, 4, 4, 5, 2]
Updated timestamp: [4, 4, 4, 5, 3]
0  [4, 4, 4, 5, 3]
15: Sent to 1: [4, 4, 4, 5, 3]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [4, 4, 4, 5, 3]
Updated timestamp: [4, 5, 4, 5, 3]
0  [4, 5, 4, 5, 3]
16: Sent to 3: [4, 5, 4, 5, 3]
0  [4, 5, 4, 5, 3]
17: Sent to 0: [4, 5, 4, 5, 3]
0  [4, 5, 4, 5, 3]
18: Sent to 1: [4, 5, 4, 5, 3]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [4, 5, 4, 5, 3]
Updated timestamp: [4, 5, 5, 5, 3]
0  [4, 5, 5, 5, 3]
19: Sent to 3: [4, 5, 5, 5, 3]
20: Sent to myself. [5, 5, 5, 5, 3]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [5, 5, 5, 5, 3]
Updated timestamp: [5, 5, 5, 6, 3]
0  [5, 5, 5, 6, 3]
21: Sent to 2: [5, 5, 5, 6, 3]
0  [5, 5, 5, 6, 3]
22: Sent to 1: [5, 5, 5, 6, 3]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [5, 5, 5, 6, 3]
Updated timestamp: [5, 5, 5, 6, 4]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [5, 5, 5, 6, 4]
Updated timestamp: [5, 6, 5, 6, 4]
23: Sent to myself. [6, 6, 5, 6, 4]
0  [6, 6, 5, 6, 4]
24: Sent to 3: [6, 6, 5, 6, 4]
0  [6, 6, 5, 6, 4]
25: Sent to 3: [6, 6, 5, 6, 4]
0  [6, 6, 5, 6, 4]
26: Sent to 3: [6, 6, 5, 6, 4]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [6, 6, 5, 6, 4]
Updated timestamp: [6, 6, 6, 6, 4]
0  [6, 6, 6, 6, 4]
27: Sent to 1: [6, 6, 6, 6, 4]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [6, 6, 6, 6, 4]
Updated timestamp: [6, 6, 6, 7, 4]
0  [6, 6, 6, 7, 4]
28: Sent to 1: [6, 6, 6, 7, 4]
0  [6, 6, 6, 7, 4]
29: Sent to 2: [6, 6, 6, 7, 4]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [6, 6, 6, 7, 4]
Updated timestamp: [6, 6, 6, 7, 5]
0  [6, 6, 6, 7, 5]
30: Sent to 2: [6, 6, 6, 7, 5]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [6, 6, 6, 7, 5]
Updated timestamp: [6, 7, 6, 7, 5]
0  [6, 7, 6, 7, 5]
31: Sent to 1: [6, 7, 6, 7, 5]
32: Sent to myself. [7, 7, 6, 7, 5]
0  [7, 7, 6, 7, 5]
33: Sent to 0: [7, 7, 6, 7, 5]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [7, 7, 6, 7, 5]
Updated timestamp: [7, 7, 7, 7, 5]
0  [7, 7, 7, 7, 5]
34: Sent to 1: [7, 7, 7, 7, 5]
0  [7, 7, 7, 7, 5]
35: Sent to 1: [7, 7, 7, 7, 5]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [7, 7, 7, 7, 5]
Updated timestamp: [7, 7, 7, 8, 5]
0  [7, 7, 7, 8, 5]
36: Sent to 1: [7, 7, 7, 8, 5]
0  [7, 7, 7, 8, 5]
37: Sent to 1: [7, 7, 7, 8, 5]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [7, 7, 7, 8, 5]
Updated timestamp: [7, 7, 7, 8, 6]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [7, 7, 7, 8, 6]
Updated timestamp: [7, 8, 7, 8, 6]
0  [7, 8, 7, 8, 6]
38: Sent to 0: [7, 8, 7, 8, 6]
0  [7, 8, 7, 8, 6]
39: Sent to 2: [7, 8, 7, 8, 6]
0  [7, 8, 7, 8, 6]
40: Sent to 3: [7, 8, 7, 8, 6]
0  [7, 8, 7, 8, 6]
41: Sent to 3: [7, 8, 7, 8, 6]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [7, 8, 7, 8, 6]
Updated timestamp: [7, 8, 8, 8, 6]
42: Sent to myself. [8, 8, 8, 8, 6]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [8, 8, 8, 8, 6]
Updated timestamp: [8, 8, 8, 9, 6]
0  [8, 8, 8, 9, 6]
43: Sent to 3: [8, 8, 8, 9, 6]
0  [8, 8, 8, 9, 6]
44: Sent to 2: [8, 8, 8, 9, 6]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [8, 8, 8, 9, 6]
Updated timestamp: [8, 8, 8, 9, 7]
0  [8, 8, 8, 9, 7]
45: Sent to 0: [8, 8, 8, 9, 7]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [8, 8, 8, 9, 7]
Updated timestamp: [8, 9, 8, 9, 7]
0  [8, 9, 8, 9, 7]
46: Sent to 3: [8, 9, 8, 9, 7]
47: Sent to myself. [9, 9, 8, 9, 7]
0  [9, 9, 8, 9, 7]
48: Sent to 3: [9, 9, 8, 9, 7]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [9, 9, 8, 9, 7]
Updated timestamp: [9, 9, 9, 9, 7]
0  [9, 9, 9, 9, 7]
49: Sent to 0: [9, 9, 9, 9, 7]
0  [9, 9, 9, 9, 7]
50: Sent to 0: [9, 9, 9, 9, 7]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [9, 9, 9, 9, 7]
Updated timestamp: [9, 9, 9, 10, 7]
51: Sent to myself. [10, 9, 9, 10, 7]
0  [10, 9, 9, 10, 7]
52: Sent to 2: [10, 9, 9, 10, 7]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [10, 9, 9, 10, 7]
Updated timestamp: [10, 9, 9, 10, 8]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [10, 9, 9, 10, 8]
Updated timestamp: [10, 10, 9, 10, 8]
53: Sent to myself. [11, 10, 9, 10, 8]
0  [11, 10, 9, 10, 8]
54: Sent to 3: [11, 10, 9, 10, 8]
0  [11, 10, 9, 10, 8]
55: Sent to 1: [11, 10, 9, 10, 8]
56: Sent to myself. [12, 10, 9, 10, 8]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [12, 10, 9, 10, 8]
Updated timestamp: [12, 10, 10, 10, 8]
0  [12, 10, 10, 10, 8]
57: Sent to 1: [12, 10, 10, 10, 8]
0  [12, 10, 10, 10, 8]
58: Sent to 2: [12, 10, 10, 10, 8]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [13, 10, 10, 10, 8]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [13, 10, 10, 10, 8]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [13, 10, 10, 10, 8]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [13, 10, 10, 10, 8]
59: Sent to myself. [13, 10, 10, 10, 8]
Updated timestamp: [13, 10, 10, 10, 9]
Updated timestamp: [13, 11, 10, 10, 9]
Updated timestamp: [13, 11, 11, 11, 9]
Updated timestamp: [13, 11, 11, 11, 9]
0  [13, 11, 11, 11, 9]
60: Sent to 3: [13, 11, 11, 11, 9]
0  [13, 11, 11, 11, 9]
61: Sent to 0: [13, 11, 11, 11, 9]
0  [13, 11, 11, 11, 9]
62: Sent to 2: [13, 11, 11, 11, 9]
0  [13, 11, 11, 11, 9]
63: Sent to 0: [13, 11, 11, 11, 9]
0  [13, 11, 11, 11, 9]
64: Sent to 2: [13, 11, 11, 11, 9]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [13, 11, 11, 11, 9]
Updated timestamp: [13, 11, 11, 12, 9]
0  [13, 11, 11, 12, 9]
65: Sent to 2: [13, 11, 11, 12, 9]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [13, 11, 11, 12, 9]
Updated timestamp: [13, 11, 11, 12, 10]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [13, 11, 11, 12, 10]
Updated timestamp: [13, 12, 11, 12, 10]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [13, 12, 11, 12, 10]
Updated timestamp: [13, 12, 12, 12, 10]
0  [13, 12, 12, 12, 10]
66: Sent to 2: [13, 12, 12, 12, 10]
0  [13, 12, 12, 12, 10]
67: Sent to 1: [13, 12, 12, 12, 10]
0  [13, 12, 12, 12, 10]
68: Sent to 2: [13, 12, 12, 12, 10]
69: Sent to myself. [14, 12, 12, 12, 10]
0  [14, 12, 12, 12, 10]
70: Sent to 1: [14, 12, 12, 12, 10]
0  [14, 12, 12, 12, 10]
71: Sent to 3: [14, 12, 12, 12, 10]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [14, 12, 12, 12, 10]
Updated timestamp: [14, 12, 12, 13, 10]
0  [14, 12, 12, 13, 10]
72: Sent to 2: [14, 12, 12, 13, 10]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [14, 12, 12, 13, 10]
Updated timestamp: [14, 12, 12, 13, 11]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [14, 12, 12, 13, 11]
Updated timestamp: [14, 12, 13, 13, 11]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [14, 12, 13, 13, 11]
Updated timestamp: [14, 13, 13, 13, 11]
0  [14, 13, 13, 13, 11]
73: Sent to 2: [14, 13, 13, 13, 11]
0  [14, 13, 13, 13, 11]
74: Sent to 2: [14, 13, 13, 13, 11]
0  [14, 13, 13, 13, 11]
75: Sent to 1: [14, 13, 13, 13, 11]
76: Sent to myself. [15, 13, 13, 13, 11]
77: Sent to myself. [16, 13, 13, 13, 11]
0  [16, 13, 13, 13, 11]
78: Sent to 0: [16, 13, 13, 13, 11]
0  [16, 13, 13, 13, 11]
79: Sent to 2: [16, 13, 13, 13, 11]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [16, 13, 13, 13, 11]
Updated timestamp: [16, 13, 13, 14, 11]
0  [16, 13, 13, 14, 11]
80: Sent to 0: [16, 13, 13, 14, 11]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [16, 13, 13, 14, 11]
Updated timestamp: [16, 13, 13, 14, 12]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [16, 13, 13, 14, 12]
Updated timestamp: [16, 13, 14, 14, 12]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [16, 13, 14, 14, 12]
Updated timestamp: [16, 14, 14, 14, 12]
0  [16, 14, 14, 14, 12]
81: Sent to 1: [16, 14, 14, 14, 12]
0  [16, 14, 14, 14, 12]
82: Sent to 2: [16, 14, 14, 14, 12]
0  [16, 14, 14, 14, 12]
83: Sent to 3: [16, 14, 14, 14, 12]
84: Sent to myself. [17, 14, 14, 14, 12]
85: Sent to myself. [18, 14, 14, 14, 12]
0  [18, 14, 14, 14, 12]
86: Sent to 2: [18, 14, 14, 14, 12]
0  [18, 14, 14, 14, 12]
87: Sent to 1: [18, 14, 14, 14, 12]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [18, 14, 14, 14, 12]
Updated timestamp: [18, 14, 14, 15, 12]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [18, 14, 14, 15, 12]
Updated timestamp: [18, 14, 14, 15, 13]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [18, 14, 14, 15, 13]
Updated timestamp: [18, 14, 15, 15, 13]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [18, 14, 15, 15, 13]
Updated timestamp: [18, 15, 15, 15, 13]
0  [18, 15, 15, 15, 13]
88: Sent to 1: [18, 15, 15, 15, 13]
0  [18, 15, 15, 15, 13]
89: Sent to 1: [18, 15, 15, 15, 13]
0  [18, 15, 15, 15, 13]
90: Sent to 0: [18, 15, 15, 15, 13]
0  [18, 15, 15, 15, 13]
91: Sent to 1: [18, 15, 15, 15, 13]
0  [18, 15, 15, 15, 13]
92: Sent to 1: [18, 15, 15, 15, 13]
0  [18, 15, 15, 15, 13]
93: Sent to 3: [18, 15, 15, 15, 13]
0  [18, 15, 15, 15, 13]
94: Sent to 1: [18, 15, 15, 15, 13]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [18, 15, 15, 15, 13]
Updated timestamp: [18, 15, 15, 16, 13]
0  [18, 15, 15, 16, 13]
95: Sent to 0: [18, 15, 15, 16, 13]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [18, 15, 15, 16, 13]
Updated timestamp: [18, 15, 15, 16, 14]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [18, 15, 15, 16, 14]
Updated timestamp: [18, 15, 16, 16, 14]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [18, 15, 16, 16, 14]
Updated timestamp: [18, 16, 16, 16, 14]
96: Sent to myself. [19, 16, 16, 16, 14]
0  [19, 16, 16, 16, 14]
97: Sent to 2: [19, 16, 16, 16, 14]
0  [19, 16, 16, 16, 14]
98: Sent to 1: [19, 16, 16, 16, 14]
0  [19, 16, 16, 16, 14]
99: Sent to 2: [19, 16, 16, 16, 14]
Finished generating events
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [19, 16, 16, 16, 14]
Updated timestamp: [19, 16, 16, 17, 14]
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [19, 16, 16, 17, 14]
Updated timestamp: [19, 16, 16, 17, 15]
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [19, 16, 16, 17, 15]
Updated timestamp: [19, 16, 17, 17, 15]
Reading from 1: [0, 0, 1, 0, 0]
Old timestamp: [19, 16, 17, 17, 15]
Updated timestamp: [19, 17, 17, 17, 15]
Notifying controller
Reading from 2: [3, 4, 3, 5, 2]
Old timestamp: [19, 17, 17, 17, 15]
Updated timestamp: [19, 17, 18, 17, 15]
Reading from 3: [1, 1, 0, 2, 0]
Old timestamp: [19, 17, 18, 17, 15]
Updated timestamp: [19, 17, 18, 18, 15]
Continuing reading...
Reading from 4: [0, 0, 0, 0, 0]
Old timestamp: [19, 17, 18, 18, 15]
Updated timestamp: [19, 17, 18, 18, 16]
Computer is tearing down.

只需找到所有以 "Reading from" 开头的语句,您就会看到模式。

我假设这与对象写入套接字以及从套接字读取错误数据有关。老实说,我不知道,这让我非常沮丧。

有什么问题,我一定会解答的!

找到了我的问题的解决方案。在将我的对象写入输出流之前,我必须添加 objectoutputstream 重置方法,以便它重置流并且不会从以前的对象回溯。

您可以在此处阅读更多相关信息:Java socket/serialization, object won't update

研究解决方案的力量!