Note that there are some explanatory texts on larger screens.

plurals
  1. POTransfering Object over Sockets-Serializable
    primarykey
    data
    text
    <p>This is the code I've written to transfer an object pm(of class PM) which contains any objects vm(of class VM) over sockets.</p> <pre><code>public class PM implements Serializable{ private static final long serialVersionUID=1L; VM vm[]=new VM[10]; //IP of the Agent String IP; public PM() { super(); for(int i=0;i&lt;10;i++){ vm[i]=new VM();} } } </code></pre> <p>VM is another class which has its own attributes.</p> <pre><code>public class VM implements Serializable{ String osType; } </code></pre> <p>The exchange of object pm over sockets takes place between 2 PCs . The server side receives the object from the client's side after the server has performed network discovery(hence the class names).</p> <pre><code>public class NetworkDiscovery extends TimerTask { InetAddress controllerIP; int controllerPort; static PM pm = new PM(); NetworkDiscovery() throws UnknownHostException { controllerIP=InetAddress.getLocalHost(); controllerPort=4455; } public void run() { try { byte[] recvBuf = new byte[5000]; DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length); DatagramSocket dSock = new DatagramSocket(4445); dSock.receive(packet); int byteCount = packet.getLength(); ByteArrayInputStream byteStream = new ByteArrayInputStream(recvBuf); ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(byteStream)); pm=(PM)is.readObject(); System.out.println("object1" +pm.IP); is.close(); dSock.close(); System.out.println("object" + pm.vm[0].vmName); } //exceptions are caught etc. } } </code></pre> <p>and on the client's side which sends the pm Object to server:</p> <pre><code>public class ackDiscovery extends TimerTask{ int agentListenPort; InetAddress agentIP; ackDiscovery(Connect c) { agentListenPort=4445; c1=c; } public void run() { ObjectOutputStream os = null; try { PM pm = new PM(); { pm.IP = InetAddress.getLocalHost().toString(); pm.vm[i].osType = d1.getOSType(); System.out.println("VMname" +i +pm.vm[i].osType); pm.vm[i].status = d1.isActive(); } InetAddress address = InetAddress.getByName("Server_IP"); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(15000); os = new ObjectOutputStream(new BufferedOutputStream(byteStream)); os.flush(); os.writeObject((PM) pm); os.flush(); byte[] sendBuf = byteStream.toByteArray(); DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length, address, 4445); int byteCount = packet.getLength(); DatagramSocket dSock = new DatagramSocket(); System.out.println("Quote of the Moment: " + pm.vm[0].osType); dSock.send(packet); os.close(); dSock.close(); } //exceptions caught etc. } } </code></pre> <p>All the vm and pm details are getting populated on the client's side(I've checked it by printing them) . on the server's side, ONLY the pm details get populated on the local pm object after transfer. displaying the vm details on server side gives me null values.</p> <p>My doubt:</p> <ol> <li>Doesn't transferring an object which has children objects also, mean that the entire parent+child objects are transferred?</li> <li>Do I have to manually transfer both vm and pm object separately?</li> </ol> <p>thanks!</p> <p>Edit- </p> <pre><code>public class VM implements Serializable{ String osType,vmName; //on server's side, these are still null int UUID,osVersion; // on the server's side, these are 0. Are integer variables initialised to a default of zero? } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload