简体   繁体   English

Java套接字:接收错误的布尔值

[英]Java Socket: receiving wrong boolean

I have a client/server architecture that sends class-instances via sockets. 我有一个通过套接字发送类实例的客户端/服务器体系结构。

In one class I have a boolean: 在一个类中,我有一个布尔值:

public class Survey implements Serializable {
    private static final long serialVersionUID = -1156493488498723461L;
    private boolean isExpired;

    public Survey() {
        this.isExpired = false;
    }

    public void markAsExpired() {
        this.isExpired = true;
    }

    public boolean isExpired() {
        return isExpired;
    }
}

The part where I send the packet: 我发送数据包的部分:

survey.markAsExpired();
HashMap<Header, Object> packet = new HashMap<Header, Object>();
packet.put(header, survey);

System.out.println(survey.isExpired()); // prints true
try {
    socketOutput.writeObject(packet);
    socketOutput.flush();
} catch (IOException e) {
    e.printStackTrace();
}

When I send that class with the boolean set to true (see above), the client always receives it as false. 当我在将布尔值设置为true的情况下发送该类时(请参见上文),客户端总是将其接收为false。

Where does this come from? 这是从哪里来的?

I could solve the problem by sending a deep copy instead of the object itself. 我可以通过发送深层副本而不是对象本身来解决问题。 Workaround but worked. 解决方法,但可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM