I’ve been trying to send more than one instance of Properties over a socket connection using PrintWriter/BufferedWriter for sending and InputStreamReader for reading, all sent and received over loop.
Sender:
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
for (int i = 0; i < vector.size(); i++) {
Properties mail = (Properties) vec.get(i);
mail.store(bw, line);
bw.newLine();
bw.flush();
}
Receiver:
ireader = new InputStreamReader(socket.getInputStream());
Properties[] mails=new Properties[c];
for (int i = 0; i < c; i++) {
Properties p;// = new Properties();
mails[i] = new Properties();
mails[i].load(ireader);
}
But I’m only receiving the last Properties object sent by the receiver only after its entire loop is done with. That’d mean the receiver is loading until the socket eventually closes. The documentation does say that for load(), the underlying reader is left open after returning, but I guess I’ve missed the part explaining ‘when’ it actually returns. How can I read multiple Properties with one stream?
Let’s suppose you have 2 Properties instances:
A:
B:
If you send these two properties instances, what will be sent on the wire will be:
And the receiver doesn’t have any way to know that this constitutes two different instances. It reads all the properties until the end of the stream, and stores all the read properties in a single object.
You need to find another protocol to send those two objects.