Solved

object serialization

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
Java string replace 11 54
Android development question 2 53
Is Applet the way to go for my drag and drop system? 8 25
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

840 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