Solved

object serialization

Posted on 2001-07-09
4
174 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 53
eclipse shortcuts 9 45
javap bin 2 30
hibernate insert example 13 15
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

863 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

23 Experts available now in Live!

Get 1:1 Help Now