Anton Giertli
Anton Giertli

Reputation: 936

How to send list via sockets java

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 ?

Upvotes: 0

Views: 1547

Answers (4)

user1225148
user1225148

Reputation: 116

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

Upvotes: 2

phatfingers
phatfingers

Reputation: 10250

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

Upvotes: 3

Joni
Joni

Reputation: 111259

ArcObject class cant implements Serializable, since it already implements Observer

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

Upvotes: 2

trutheality
trutheality

Reputation: 23465

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.

Upvotes: 2

Related Questions