Java InputStream throwing java.io.StreamCorruptedException - java

I have a problem that I do not quite understand. I have a server socket and a client socket on two separate machines. Server will constantly sends out data in hex and connected client will read the data stream and do some work.
Most of the traffic is happening in that manner. On a few rare occasions, client will send a bytestring to Server. At this point, I am getting
Java.io.StreamCorruptedException at this line:
ObjectInputStream in = new
ObjectInputStream(socketFromClient.getInputStream());
Exception:
java.io.StreamCorruptedException: invalid stream header: 020001B5
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:857)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:349)
at ReadInput.run(ReadInput.java:21)
at java.lang.Thread.run(Thread.java:745)
"020001B5" is exactly the bytestring that client is sending over to server.
I do not understand why I am getting this StreamCorruptedException. My
code logic does not have any "stream header". What is stream header?
I checked javadoc and I could not make out of why this is happening.
Any tip much appreciated.
thanks
-v

Your client is not writing data using ObjectOutputStream: that's why you're seeing this exception. ObjectOutputStream and ObjectInputStream are used for serializing and deserializing Java objects.
Instead of using ObjectInputStream to read data, consider using BufferedReader or a DataInputStream.
See this example for more details.

Related

Java won't deserialize object after it is sent through socket stream

I'm trying to create application that will send object through local network using Sockets. When i run server and client code in Intellij Idea they work fine, but when i run server code on one pc and client code on another pc i get errors like java.io.StreamCorruptedException: invalid type code: 00 or java.io.StreamCorruptedException: invalid stream header: 6C69656E
byte[] readBuffer = new byte[4096];
int num = inStream.read(readBuffer); //inStream is socket input stream
ByteArrayInputStream bis = new ByteArrayInputStream(readBuffer);
ObjectInput in = new ObjectInputStream(bis);
Object o = in.readObject(); //this line throws error
The thing is that writing and reading object to socket stream works on server (which is on pc where i created project) but reading from input stream on client (another pc where i copied project) throws error.
Can someone help me with this? I searched everywhere for solution but i can't figure out what is problem with serializing, because it works on same pc but won't on another. Is there any way that i can make this pc independent? This also happens when i create jar files and run it on same pc where it works in Intellij Idea.
It can because that client didnt read message fully.
But the real mistake is that you work with TCP socket like a message protocol transport but TCP is a stream protocol so you have to create your own message protocol on top of TCP.
Why it works fine on local system?
Because transport data between client and server happen too fast in local test and maybe in just one frame so all the message transported in just one IO-call but in internet or a network it doesn't work like you think.
There is 2 way to handle this mistake:
1- Pass SocketInputStream directly to ObjectInputStream instance and let it handle read objects.
2- Create a message protocol for example you can put the size of message in 2 or more first bytes. Then you can workd like this :
Read 2(or more) first bytes and detect size of packet.
Create a buffer for this size and read packet bytes.(make sure you read all of packet data from socket . You can use return value of SocketInputStream.read(byte[]) method to calculate it)
Pass the packet to ObjectInputStream and read object !

CorruptedFrame exception , websockets in Netty

I was just playing around the websocket example client and server on netty wiki.
I modified the way server sends data to client.
Suddenly i started getting exception like "io.netty.handler.codec.CorruptedFrameException: bytes are not UTF-8"
This is on client and i am creating frame on server like this :-
ByteBuf buf = Unpooled.buffer();
buf.writeShort(1);
channel.write(new TextWebSocketFrame(buf))
I understand from error that bytebuf must be UTF-8 encoded , even the constructor of TextWebSocketFrame says this.
But i have no clue on why i am getting this error.
Any suggestions ?
You are getting this as you try to send non UTF-8 data. If you want to just send any binary data use a BinaryWebSocketFrame.

ObjectInputStream - StreamCorruptedException:unexpected end of block data

I have a server and client applications.
The client and server are in the same machine.
The client and server are using same java version.
I'm using ObjectOutputstream /objectInputStream to write and read
the objects
The transmitted object is notbeing modified while writing
the code :
mySocketClient.getOos().writeObject((replyobject));
mySocketClient.getOos().flush();
mySocketClient.getOos().reset();
Reading Code :
objectInputStream.readObject();
The application is working good but randomly I have the below exception
java.io.StreamCorruptedException: unexpected end of block data
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
What might be causing this?

How to redirect input and output of a Process over a TCP socket connection? (java)

I'd like to know how I can redirect the input and output of a system process over a TCP connection from the server-side. This would result in the client being able to interact with said process.
So far I know how to actually establish a simple TCP connection, and I can get the input and output streams of the connection socket using the getInputStream() and getOutputStream() methods.
I can also get the output and input streams of a Process (after initializing the process via ProcessBuilder.start() method) using its getInputStream() and getOutputStream() methods.
So I end up with two sets of input and output streams. What I want to know is how to connect (may be using the wrong word here) the output and input streams of the Process with the output stream of the connection socket, so that the client on the other side of the TCP connection can interact with the Process.
If it matters, I'm using BufferedInputStreams and BufferedOutputStreams.
Any information regarding this problem is greatly appreciated, thanks!
To redirect the output streams of a process to socket, try the following code
System.setOut( new PrintStream(mySocket.getOutputStream(),true))
Visit
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#setOut%28java.io.PrintStream%29
Similarly, explore the System.setErr() and System.setIn() for error and input streams respectively!

java.io exception in ObjectInputStream

I have connected my server by gps device. And for reading data I'm using the code
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
that produces the following exception:
java.io.StreamCorruptedException: invalid stream header: 24312C38
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at com.tm4u.ServerSocketExample.main(ServerSocketExample.java:34)
The exception suggests that the data stream you're getting from the socket is not a valid object stream. Object streams are intended to read Java objects that were serialized by a Java process. If you're reading data from a GPS device, I doubt that's the type of stream the device is sending.
I'm sure you need to use DataInputStream or BufferedInputStream with InputStreamReader to read data from GPS device.
ObjectInputStream is used read Java objects sent by other application through the ObjectOutputStream. These are less compatible streams. For example: JVM object serialisation protocol must be the same. I doubt that GPS device manufacturer designed this device to be used in this way.

Categories