I want to send data which i have serialized over a TCP connection. I have created a client/server connection and am sending an Object after serializing it. However, I dont know how should i read the data.
here is the code snippet:
SENDING FUNCTION:
sendTo(String receiverAddr, int receiverPort,....., Object data) {
.
.
.
if (data != null) {
byte[] byteObj = programming5.io.Serializer.serializeBytes(data);
output.writeInt(byteObj.length);
output.write(byteObj, 0, byteObj.length);
output.flush();
}
output.close();
sock.close();
}
Function Call:
String hostname = somevalue;
int portNo = somevalue;
Hashtable <Integer, Integer&g开发者_如何学编程t; object = somevalue;
sendTo(hostname,portNo,...,object);
Receiving Function:
DataInputStream input = new DataInputStream(clientSocket.getInputStream());
int length = input.readInt();
byte[] bytes = new byte[length];
input.readFully(bytes);
Hashtable<Integer, Integer> recvObj = (Hashtable<Integer,Integer)programming5.io.Serializer.deserialize(bytes);
This is not working. I am getting the following exception:
invalid stream header: 07F8ACED java.io.StreamCorruptedException: invalid stream header: 07F8ACED
Please tell me how should i go about this.
Use ObjectOutputStream
to write objects and ObjectInputStream
to read them.
精彩评论