简体   繁体   中英

Object ArrayList over socket

I have to pass an ArrayList over tcp socket (Server > Client) over the network. When I pass and Arraylist of Strings it works fine and the client is able accept the ArrayList and and print. but when the ArrayList is off Objects I have many exceptions and it does not work. Have a look below please.

Server

    ArrayList<Database> List = new ArrayList<>(); //Database is a seperate class
    Database item1 = new Database();
            item1.code = "1568";
    item1.name = "Round Table";
    item1.details = "Ikeas best Seller";
    item1.inStock = "17";
    List.add(item1);

...

ObjectOutputStream oout = new ObjectOutputStream(socket.getOutputStream());
oout.writeObject(List);

Client

ObjectInputStream iin =  new ObjectInputStream(socket.getInputStream());
Object items = iin.readObject();

Using this code I have many Exceptions thrown. If I replace the array with a string type array like ArrayList<String> Items = new ArrayList<>(); It will work fine. Any Ideas? I am really stack here. Thank You!

The object you are putting in the list and sending must implement the Serializable interface. That also means that fields within that class will also have to be Serializable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM