• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending LinkedList with Socket  RSS feed

 
Lina Mahl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I have a very big problem. Data is getting lost between my client and my server.
I have an Object (a RecipeObject with a LinkedList with IngredientObjects) and when I put it in another Object (Envelope) and send it to the server the first time. Everything is god.
But if I add something to my LinkedList and send the whole thing again the LinkedList doesn�t change on the server side.
iToServer = new ObjectOutputStream(iSocket.getOutputStream());
...
Envelope tEnvelope = new Envelope();
tEnvelope.setWhatToDoStrin(tEnvelope.ADD_RECIPE);
tEnvelope.setObject(aRecipe);
...
iToServer.writeObject(aEnvelope);
When I print the hashCode for The Envelope the Recipe and the LinkedList on both the client side and the server side. I can se that the hash code has changed for every object but the LinkedList on the server side.
What is the problem? I have been given the tip to write my own hashCode for the IngerdientObject that is in the LinkedList. But how do I do that? I have tried but nothing seems to happen.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suspect a coding problem at the receiving end. Can you give us some more details (code snippets) on that?
- Peter
 
Lina Mahl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That�s just it. On the server I just look at the Object read from the input stream. And it is not the same object that I sent.
Here is some of my code. But I am just on my way to test the system in an other way. Maybe it wont work when the client and the server is on the same computer (local 127.0.0.1)
Server side :
/**
* ServerContactManager is the one class that communicates with the presentation package.
* It opens a server socket and creates a new CookbookManager object for every client that
* connect.
*/
public class ServerContactManager {

while (true) {
iSocket = iServerSocket.accept();
CookbookManager tListener = new CookbookManager(iSocket, this);

}
class CookbookManager implements Runnable {
..
public CookbookManager(Socket aSocket, ServerContactManager aServer) {
iSocket = aSocket;
iServer = aServer;
iKeepRunning = true;
try {
iFromClient = new ObjectInputStream(iSocket.getInputStream());
iToClient = new ObjectOutputStream(iSocket.getOutputStream());

public void run() {
while(iKeepRunning) {
lookAtInputStream();
}
}
..
public synchronized void lookAtInputStream() {
System.out.println("Looking at input");
try {
Object tObject = iFromClient.readObject();

if (tObject instanceof Envelope) {
Envelope tEnvelope = (Envelope) tObject;
String tWhatToDo = tEnvelope.getWhatToDo();

if (tWhatToDo.equals(tEnvelope.ADD_RECIPE)) {

addRecipe((Recipe) tEnvelope.getObject());
// Here the recipe isn�t the same even thou I haven done anything with it.
// Maybe because the client and server is on the same computer in reality
}

public void addRecipe(Recipe aRecipe) {
CookbookXMLWriter tXMLWriter = new CookbookXMLWriter();
tXMLWriter.addRecipe(aRecipe);
..
}
 
Lina Mahl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No It won�t work. I have a recipe with one igredient in a LinkedList and I add an ingredient and send it to the server but the changed LinkedList dosen�t make it. And I�m not doing anything in between.
So I give upp. No editing of recipes in my cookbook. Make them or delete them and be happy.
If someone is interested the code is here
http://atlas.dsv.su.se/~x02-klj/10/TheApplication

/Lina
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!