简体   繁体   中英

Serialized Object loses data

I created a Java Server and Client applications that communicates by RMI. In order for the Client to "meet" the server, i'm sending a multicast DatagramPacket to the Client. What happens is that i instantiate the class with the attribute localDirectory and when i recieve it, it comes has null. Important to say that the object that i'm trying to send implements Seriaizable.

What do you think i'm doing wrong?

(if you need aditional code let me know)

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:

  • was null when serialized
  • is static
  • is transient
  • isn't written by a custom writeObject() method
  • isn't assigned by a custom readObject() method
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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