S
Steffen Heinzl
Hi!
I got a a problem sending compressed objects via sockets.
At the client site a Message object is given to the static method
sendMessage()
There are a few other MessageTypes which extend Message, so I can send
different types of messages over the socket.
My OutputStream os was obtained by socket.getOutputStream(), that means the
message objects are given to "os" and thus sent over the socket.
The variable compressed indicates whether GZIP is to be used or not.
If compressed is always set to false, there are no problems.
If compressed is set to true, I get an IOException (at server side) after an
undefined number of sent messages. Sometimes only 1 or 2 messages are
received correctly, sometimes 20.
The IOException looks like this:
java.io.IOException: Not in GZIP format
at
java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:131)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:58)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:68)
at
ScreenGrabber.messaging.MessageLogic.readMessage(MessageLogic.java:68)
at
ScreenGrabber.communication.ConnectionThread.run(ConnectionThread.java:80)
At the client site a thread is responsible for sending messages; at the
server site a thread is used to receive messages.
The code for sending and receiving messages:
client site:
public static synchronized void sendMessage(Message msg, OutputStream os,
boolean compressed) throws IOException
{
if(compressed)
{
GZIPOutputStream gout = new GZIPOutputStream(os);
ObjectOutputStream oos = new ObjectOutputStream(gout);
oos.writeObject(msg);
gout.finish();
}
else
{
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(msg);
}
}
server site (InputStream is socket.getInputStream()):
public static synchronized Message readMessage(InputStream in, boolean
compressed) throws ClassNotFoundException, IOException
{
if(compressed)
{
GZIPInputStream gin = new GZIPInputStream(in);
ObjectInputStream ois = new ObjectInputStream(gin);
return ((Message) ois.readObject());
}
else
{
ObjectInputStream ois = new ObjectInputStream(in);
return ((Message) ois.readObject());
}
}
Can anybody tell me why the compression doesn't work properly?
(I'm using jdk 1.4.2)
Thanks in advance,
Steffen
I got a a problem sending compressed objects via sockets.
At the client site a Message object is given to the static method
sendMessage()
There are a few other MessageTypes which extend Message, so I can send
different types of messages over the socket.
My OutputStream os was obtained by socket.getOutputStream(), that means the
message objects are given to "os" and thus sent over the socket.
The variable compressed indicates whether GZIP is to be used or not.
If compressed is always set to false, there are no problems.
If compressed is set to true, I get an IOException (at server side) after an
undefined number of sent messages. Sometimes only 1 or 2 messages are
received correctly, sometimes 20.
The IOException looks like this:
java.io.IOException: Not in GZIP format
at
java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:131)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:58)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:68)
at
ScreenGrabber.messaging.MessageLogic.readMessage(MessageLogic.java:68)
at
ScreenGrabber.communication.ConnectionThread.run(ConnectionThread.java:80)
At the client site a thread is responsible for sending messages; at the
server site a thread is used to receive messages.
The code for sending and receiving messages:
client site:
public static synchronized void sendMessage(Message msg, OutputStream os,
boolean compressed) throws IOException
{
if(compressed)
{
GZIPOutputStream gout = new GZIPOutputStream(os);
ObjectOutputStream oos = new ObjectOutputStream(gout);
oos.writeObject(msg);
gout.finish();
}
else
{
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(msg);
}
}
server site (InputStream is socket.getInputStream()):
public static synchronized Message readMessage(InputStream in, boolean
compressed) throws ClassNotFoundException, IOException
{
if(compressed)
{
GZIPInputStream gin = new GZIPInputStream(in);
ObjectInputStream ois = new ObjectInputStream(gin);
return ((Message) ois.readObject());
}
else
{
ObjectInputStream ois = new ObjectInputStream(in);
return ((Message) ois.readObject());
}
}
Can anybody tell me why the compression doesn't work properly?
(I'm using jdk 1.4.2)
Thanks in advance,
Steffen