fxlemire fxlemire - 4 days ago 5
Java Question

Java object LinkedList attribute: only receiving the first element on server-side using TCP

A little bit of context: the client is sending to the server a SOSPFPacket object (via TCP) that has various attributes, such as a

Vector<LSA> lsaArray
. The
LSA
itself has a
LinkedList<LinkDescription> links
attribute. In my test case, there are two messages being sent. In both messages, there is only one
LSA
in the vector. In the first message, the LSA has one LinkDescription, in the second, it has two. When I send a message, I increment the
messageId
.

The server receives both messages with proper ids, but in the second message, the links only contain one link instead of two. I'm clueless...

Here are the object implementations:

import java.io.*;
import java.util.Vector;

public class SOSPFPacket implements Serializable {
public final static short HELLO = 0;
public final static short LSU = 1;
public final static short OVER_BURDENED = 2;
public static int id = Integer.MIN_VALUE;

public String srcProcessIP;
public short srcProcessPort;
public String srcIP;
public String dstIP;
public short sospfType; //0 - HELLO, 1 - LinkState Update, 2 - Over Burdened
public String routerID;
public int messageId = id++;
public String neighborID; //neighbor's simulated IP address
public Vector<LSA> lsaArray = new Vector<>();
public String lsaInitiator = null;
}





import java.io.Serializable;
import java.util.LinkedList;

public class LSA implements Serializable {
public String linkStateID;
public int lsaSeqNumber = Integer.MIN_VALUE;
public LinkedList<LinkDescription> links = new LinkedList<LinkDescription>();

@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(linkStateID + ":").append(lsaSeqNumber + "\n");
for (LinkDescription ld : links) {
sb.append(ld);
}
sb.append("\n");
return sb.toString();
}
}





import java.io.Serializable;

public class LinkDescription implements Serializable {
public String linkID;
public int portNum;
public int tosMetrics;

public LinkDescription() {}

public LinkDescription(String linkID, int portNum, int tosMetrics) {
this.linkID = linkID;
this.portNum = portNum;
this.tosMetrics = tosMetrics;
}

public String toString() {
return linkID + "," + portNum + "," + tosMetrics;
}
}





To send the message, I do it via a
Client.java
thread implementing
Runnable
. Here are the relevant methods:

public void run() {
try {
_outputStream = new ObjectOutputStream(_clientSocket.getOutputStream());
sendMessage(SOSPFPacket.HELLO);
_inputStream = new ObjectInputStream(_clientSocket.getInputStream());
SOSPFPacket message = Util.receiveMessage(_inputStream);

if (message.sospfType == SOSPFPacket.OVER_BURDENED) {
System.out.println("Removing link with router " + message.srcIP + "...");
_router.removeLink(_remoteRouterIP);
return;
}

_remoteRouterDescription.setStatus(RouterStatus.TWO_WAY);
_router.addLinkDescriptionToDatabase(_remoteRouterDescription, _link.getWeight());
sendMessage(SOSPFPacket.HELLO);
message = Util.receiveMessage(_inputStream);

if (message.sospfType == SOSPFPacket.LSU) {
_router.synchronize(message.lsaArray);
}

_router.propagateSynchronization(message.lsaInitiator, message.srcIP);
} catch (IOException e) {
e.printStackTrace();
}
}

private void sendMessage(short messageType) {
try {
SOSPFPacket message = Util.makeMessage(_rd, _remoteRouterDescription, messageType, _router);
_outputStream.writeObject(message);
_outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}





public class Util {
public static SOSPFPacket makeMessage(RouterDescription local, RouterDescription external, short messageType, Router rd) {
SOSPFPacket message = new SOSPFPacket();
message.srcProcessIP = local.getProcessIPAddress();
message.srcProcessPort = local.getProcessPortNumber();
message.srcIP = local.getSimulatedIPAddress();
message.dstIP = external.getSimulatedIPAddress();
message.sospfType = messageType;
message.routerID = local.getSimulatedIPAddress();
message.neighborID = external.getSimulatedIPAddress();
rd.getLsd().getStore().forEach((k, v) -> message.lsaArray.addElement(v));
message.lsaInitiator = messageType == SOSPFPacket.LSU ? message.srcIP : null;

return message;
}

public static SOSPFPacket receiveMessage(ObjectInputStream inputStream) {
SOSPFPacket receivedMessage = null;

try {
receivedMessage = (SOSPFPacket) inputStream.readObject();

String messageType;

switch (receivedMessage.sospfType) {
case SOSPFPacket.HELLO:
messageType = "HELLO";
break;
case SOSPFPacket.LSU:
messageType = "LINKSTATEUPDATE";
break;
case SOSPFPacket.OVER_BURDENED:
messageType = "OVER_BURDENED";
break;
default:
messageType = "UNKNOWN_STATE";
break;
}

System.out.println("received " + messageType + " from " + receivedMessage.srcIP + ";");
} catch (ClassNotFoundException e) {
System.out.println("No message received.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return receivedMessage;
}
}





And the server instantiates a private
ClientServiceThread
when it receives a new connection, which is in charge of receiving the message.

private class ClientServiceThread implements Runnable {
Socket _clientSocket;
Thread _runner;

ClientServiceThread(Socket s) {
_clientSocket = s;
_runner = new Thread(this);
}

public Thread getRunner() { return _runner; }

public void run() {
ObjectInputStream inputStream = null;
ObjectOutputStream outputStream = null;

try {
inputStream = new ObjectInputStream(_clientSocket.getInputStream());
outputStream = new ObjectOutputStream(_clientSocket.getOutputStream());

while (true) {
try {
SOSPFPacket receivedMessage = Util.receiveMessage(inputStream);

//some logic not relevant since the receivedMessage is already not correct
}
}
}
}
}


Again, all
SOSPFPacket
fields are correctly received, except for the
Vector<LSA> lsaArray
...




Edit: I also tried sending a third
sendMessage(SOSPFPacket.HELLO)
after
_router.propagateSynchronization(message.lsaInitiator, message.srcIP);
. This time, the message being sent contains two
LSA
, the first one having two
LinkDescription
, the second one having one. Both
LSA
are received by the server, but still, only the first
LinkDescription
is received in the first
LSA
. The message id is correct in all three messages.
If I run everything a second time (i.e. I create a new Client and a new ClientService Thread for the already running routers), only then does the server finally receive two
LinkDescription
in the first
LSA
.

EJP EJP
Answer

Java sends references to objects that have already been serialized, to preserve the integrity of object graphs.

You should call ObjectOutputStream.reset() after each writeObject().

Or use ObjectOutputStream.writeUnshared(), but note that it still shares referenced objects, i.e. if you try to send a list with both added and changed element objects, it will send the new list and new element objects, but not the element objects which have been changed.

Comments