Solved

object serialization

Posted on 2001-07-09
4
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 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