Question

I got an Object which I want to send over a Socket. For the Ouput I got:

Socket s = serverSocket.accept();
ObjectOutputStream dout = new ObjectOutputStream(new GZIPOutputStream(s.getOutputStream()));
Msg m = new Msg("123", "123", karte);
dout.writeObject(m);
dout.reset(); //No I don't want to chache it

and for the Input I got:

Socket s = new Socket(host, port);
ObjectInputStream din = new ObjectInputStream(new GZIPInputStream(s.getInputStream()));
Msg m = (Msg)din.readObject();

the Msg Object is Serializeable.

If I run this code, I get an Client connect, the Server also sends data, but the client never starts to read the input

any suggestions?

Thanks for all replys

Was it helpful?

Solution

Maybe flush the output stream before calling reset() ?

Oh yeah, one other thing, you might need to close the GZIP output first, I believe it does not finalize the gzip stream before close() is called. In which case you might have to close the whole stream stack you have going there and reopen it. Maybe consider handling the compression separately and only pushing the result to the output stream?

Some sample code:

Socket s = serverSocket.accept();
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
GZIPOutputStream compressor = new GZIPOutputStream(compressed);

Msg m = new Msg("123", "123", karte);
m.writeObject (compressor);
compressor.close();

OutputStream fout = s.getOutputStream();
fout.write (compressed.toByteArray() );
fout.close ();

IF you can't close the stream, it gets slightly trickier. If the server does not close the stream, you do not get the EOS signal.This means you will have to accumulate incoming bytes in a buffer (ByteArrayOutputStream is handy), and manually scan the bytes as they come in for some end sequence, so that you know when to stop reading and start deserializing.

The above code comes with no warrantee of of course... :p

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top