I am trying to send a custom Object (Info) using a socket from a server to a client. Info class code is:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class Info implements Serializable {


    private static final long serialVersionUID = -8967456786256719218L;


    private String ip;
    private int port;
    private List<String> responsibilityLines;


    public Info(String ip, int port, List<String> responsibilityLines) {
        this.ip = ip;
        this.port = port;
        this.responsibilityLines = responsibilityLines;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getSubscriberPort() {
        return port;
    }

    public void setSubscriberPort(int port) {
        this.port = port;
    }

    public List<String> getResponsibilityLines() {
        return responsibilityLines;
    }

    public void setResponsibilityLines(ArrayList<String> responsibilityLines) {
        this.responsibilityLines = responsibilityLines;
    }

    @Override
    public String toString() {
        return "Info{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", responsibilityLines=" + responsibilityLines +
                '}';
    }
 }

The socket connection is established and any other java object or primitive type is sent successfully. However when I try to send a List of Info objects, the client blocks and wait to receive (which never happens). To send the object from the server I use:

 List<Info> infoList = new ArrayList<>();
 Info inf = new Info("192.168.78.45", 3456, null);
 infoList.add(inf);
 dos.writeObject(infoList);
 dos.flush();

and to receive the object I use:

List<Info> infoList;
infoList = (List<Info>) dis.readObject();

where dos is the ObjectOutputStream and dis is the ObjectInputStream.

1 Answers

0
Murtaza Khursheed Hussain On

Use ByteArrayOutputStream as an inpute to ObjectOutputStream()

ByteArrayOutputStream bos = new ByteArrayOutputStream()
ObjectOutputStream oos = new ObjectOutputStream(bos)
oos.writeObject(infoList)
oos.close

given that your server knows who to parse info class.