How to send list via sockets java

Go To StackoverFlow.com

0

i am trying to send an Object via socket.

its object Net

public class Net {
    public List<NetObject> objects; //= new ArrayList<NetObject>(); // place + transition
    public List<ArcObject> arcs; // = new ArrayList<ArcObject>(); // arcs - objects
}

here is the ArcObject class

public class ArcObject implements Observer  {

    public NetObject o1;
    public NetObject o2;
    public String parameter;
}

and here is NetObject class

public class NetObject implements Observer{
public int index; // index of object
public int type; // type - place=1, transition=2 ...
public int x; // position
public int y;
public List<Integer> tokens ; //list of tokens
//public List<ArcObject> arcs = new ArrayList<ArcObject>();
public String guard;
// etc... 
}

then i connect to the server

        String computername=InetAddress.getLocalHost().getHostName();
        kkSocket = new Socket(computername, 4444);
        OutputStream outputStream = null ;
        ObjectOutputStream  out = null ; 
        outputStream = kkSocket.getOutputStream();  
        out = new ObjectOutputStream(outputStream);  

and then i try to send object via socket

out.writeObject(petriNet); //petriNet object is from class Net

but the client gives me an exception

java.io.NotSerializableException: petri.ArcObject

but ArcObject class cant implements Serializable, since it already implements Observer, so how am i supposed to send object via socket which has two lists included. any ideas ?

2012-04-05 18:28
by Anton Giertli


3

You're allowed to implement multiple interfaces. Just comma-separate them in the declaration.

2012-04-05 18:30
by phatfingers


2

ArcObject and all its members (and their members and so on) need to implement the Serializable interface (it's just a marker interface, no methods to implement).

Oh, and, you can implement multiple interfaces. What you can't do is extend multiple classes.

2012-04-05 18:29
by trutheality


2

ArcObject class cant implements Serializable, since it already implements Observer

Yes, it can. A class can implement several interfaces.

2012-04-05 18:30
by Joni


2

You can actually implement more than one interface in Java. Therefore it is possible to implement Observer AND Serializable.

2012-04-05 18:31
by user1225148
Ads