Solved

object serialization

Posted on 2001-07-09
4
162 Views
Last Modified: 2010-05-18
Hi ,

I have a java client and a c server

I am trying to use object serialization
to send the object to the server over the socket


The problem is that the data received by server
is all junk.

The client code is as follows:

class Test implements Serialization

Test obj = new Test("hello" ,20);

clientSocket = new Socket(host, portNo); //open a socket

outSock  = new DataOutputStream (
            clientSocket.getOutputStream());

out = new ObjectOutputStream(outSock);

out.writeObject(obj);
out.flush();

when I debug and see the value received by the server
it is all junk

what am i missing?

0
Comment
Question by:sanjay_thakur
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6266858
Hi,

See also: http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20147995
You are missing a lot. Object serialization is designed to preserve type information and topology of complex object networks, so that when you serialize object A which refers objects B and C and both of ther refer object D, you will get same diamond structure when deserialize them. And types of objects will be restored. So, java serialization writes type description for each serialized type (name, names and types of serialized fields) and type information for each serialized object. Instead of already written objects, special back-reference is written.

When you get to field values, note that strings are written in 'modified UTF-8' format and numbers are written in big-endian byte order.

So, it's not a junk, it's simply hard to interpret.

Regards,
Igor Bazarny
Brainbench MVP for Java 1
www.brainbench.com
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6267536
Hi,

My problem is that the client is Java
and the server is C

I know if my server is Java there is no encoding/decoding problem

But how will a c program understand the encoding
done by java?

basically I have a structure
I map this structure to a java file
where instance variables of the class match the fields
in my struct


now I create an object and I want to write this
object on the Socket

(I am doing this so that the server can get all
 the values at once and fill the corresponding struct)


How do I do this because If I use WriteObject()
then I will need to decode on the server end
which is not feasible



any help?
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6267946
I posted same question twice
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 100 total points
ID: 6270933
Hi,

I would just use DataOutputStream to transfer only data fields, not class information, layout etc. If you have a few structures which are not likely to change (you don't need type check at runtime, you can code serialization separately for each type of structure), then lower level (DataOutputStream) is better choice. Even in that case you need to take care of data formats on c side: java reads and writes ints in big-indian byte order (most significant byte first), and strings in slightly modified UTF-8 encoding by default. String encoding you can tune at java side using OutputStreamWriter and ByteArrayOutputStream.

Regards,
Igor Bazarny
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now