Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

object serialization

Posted on 2001-07-09
4
Medium Priority
?
211 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 300 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

Industry Leaders: 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 Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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