简体   繁体   English

序列化对象丢失数据

[英]Serialized Object loses data

I created a Java Server and Client applications that communicates by RMI. 我创建了一个通过RMI进行通信的Java Server和Client应用程序。 In order for the Client to "meet" the server, i'm sending a multicast DatagramPacket to the Client. 为了让客户端“满足”服务器,我正在向客户端发送多播DatagramPacket。 What happens is that i instantiate the class with the attribute localDirectory and when i recieve it, it comes has null. 发生的事情是我使用属性localDirectory实例化类,当我收到它时,它来自null。 Important to say that the object that i'm trying to send implements Seriaizable. 重要的是,我正在尝试发送的对象实现了Seriaizable。

What do you think i'm doing wrong? 你觉得我做错了什么?

(if you need aditional code let me know) (如果您需要aditional代码让我知道)

public static void main(String[] args) throws RemoteException {




localDirectory = new File("c:\\tempfiles".trim());

serverToTransmit = (ServerInterface) new Server(localDirectory);


if (!localDirectory.exists()) {
    System.out.println("Directory " + localDirectory + " does NOT exist!");
    return;
}

if (!localDirectory.isDirectory()) {
    System.out.println("The path " + localDirectory + " is NOT a directory!");
    return;
}

if (!localDirectory.canRead()) {
    System.out.println("WITHOUT reading permissions on " + localDirectory + "!");
    return;
}

try {
     System.out.println("Vou criar o registry");
    Registry registry;

    try {

        System.out.println("Registry lauching try in port " + 7609 + "...");

        registry = LocateRegistry.createRegistry(7609);

        System.out.println("Registry launched!");
    } catch (RemoteException e) {

        System.out.println("Registry probably in execution already!");
        registry = LocateRegistry.getRegistry();
    }

                 System.out.println("Será que criei?");

    //Create Service
    //ServerInterface service = new Server(localDirectory);
    Server server = new Server(localDirectory);

    registry.bind(SERVICE_NAME, server);

    System.out.println("Service " + SERVICE_NAME + " registered on registry.");


    MulticastSocket sock = new MulticastSocket(7000);   //225.15.15.15 e o porto 7000.
    InetAddress addr = InetAddress.getByName("225.15.15.15");
    sock.joinGroup(addr);
    //sock.setTimeToLive(1);
    DatagramSocket psock;
    while (true) {

        try {
            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bOut);
            out.writeObject(serverToTransmit);
            //out.writeUnshared(objectToTransmit) in order to avoid caching issues
            out.flush();
            byte[] data = bOut.toByteArray();
            DatagramPacket packet = new DatagramPacket(data, data.length, addr, 7000);
            System.out.println("sizeeee: " + bOut.size());
            sleep(3000);
            sock.send(packet);
        } catch (SocketException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /*   if (args.length > 0 && args[0].equals("leader")) {
    playLeader(sock, psock, addr);
} else {
    playFollower(sock, psock, addr);
}
sock.leaveGroup(addr);*/
} catch (RemoteException e) {

    System.out.println("Remote error - " + e);
    System.exit(1);
} catch (Exception e) {

    e.printStackTrace();
}

and i'm receiving it on the Client like this (only put the important code): 我在这样的客户端收到它(只提供重要的代码):

MulticastSocket sock = new MulticastSocket(7000);   //225.15.15.15 e o porto 7000.
                    InetAddress addr = InetAddress.getByName("225.15.15.15");
                    sock.joinGroup(addr);

                    sock.setTimeToLive(1);

                    System.out.println("PASSOU AQUI_!");
                    DatagramPacket packet = new DatagramPacket(new byte[1024], 1024, addr, 7000);
                    sock.receive(packet);
                     System.out.println("PASSOU AQUI_1!");
                    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(packet.getData(), 0,
                            packet.getLength()));
                     System.out.println("PASSOU AQUI_2!");
                    ServerInterface returnedObject = (ServerInterface) in.readObject();
                    System.out.println("Returned Object LOCAL DIR -> " + returnedObject.getLocalDirectory());

Thanks! 谢谢!

So, to cover all the possibilities include some excluded by your question, the attribute localDirectory either: 因此,为了涵盖所有可能性,包括一些被您的问题排除的可能性,属性localDirectory

  • was null when serialized 序列化时为null
  • is static static
  • is transient transient
  • isn't written by a custom writeObject() method 不是由自定义writeObject()方法编写的
  • isn't assigned by a custom readObject() method 未由自定义readObject()方法分配
  • wasn't even present in the version of the class that got serialized but was in the version that got deserialized 甚至没有出现在序列化的类的版本中,但是在反序列化的版本中
  • is lost somewhere in a writeReplace()/readResolve() megillah. writeReplace()/readResolve() megillah中丢失了。

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

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