1

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

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
nZeloT
  • 63
  • 1
  • 2
  • 7
  • 1
    I don't think reset does what you think it does... "Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again." Have you tried just flushing + closing the stream? – eis Jan 28 '13 at 15:01
  • Well the problem is, I need the stream to be open, and before i didn't add the compression, it worked well – nZeloT Jan 28 '13 at 15:10
  • please try first with just flushing + closing the stream, and update with the results. Even if you would need the stream to be open it is valuable in solving the issue to know if it works then or not. – eis Jan 28 '13 at 15:15
  • works if i close the stream – nZeloT Jan 28 '13 at 15:30

1 Answers1

2

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

demaniak
  • 3,716
  • 1
  • 29
  • 34
  • could you post some sample code pls? Actually also tried this with sending a byte Array, but then i got something to read, but the input was not complete – nZeloT Jan 28 '13 at 15:06
  • Uh, if you need the streams to remain open, you don't HAVE to do do the last close(), but at least call flush() – demaniak Jan 28 '13 at 15:15
  • Oh yes, you will probably have to adjust the client along similar lines, but it might actually still work. – demaniak Jan 28 '13 at 15:18
  • Well it works, but if i try to read the send data, i got an error because at the moment of the tried read the object is not completely transfered – nZeloT Jan 28 '13 at 15:28
  • Yikes. Ok. That is now where it gets tricky. 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. – demaniak Jan 28 '13 at 15:53