Sending object over network WITHOUT using serializable interface

S

Steve Line

Hi everybody,

I've read many articles about doing this WITH serializable, but using
serializable won't do what I need. I have a java program that must
talk to a box and I don't have access to the sw on this box. I need
to send a datagram with a C structure in it to the box to control it.
The C structure is about 15 or so shorts and ints. I'm converting the
C to Java and need to send the same data format from a java program.
I've defined a Java Class consisting of the ints and shorts in the
right order. Now I want to stick it in a DatagramPacket and send it.
Problem is all the Java serializable capabilities stick other junk
like class id into the output stream. How do I do it and JUST SEND
THE DATA, i.e. the ints and shorts like any old school C or C++
programmer would do? (Also I don't want to use JNI -- too many cygwin
problems).

Steve
 
M

Mike Schilling

Steve Line said:
Hi everybody,

I've read many articles about doing this WITH serializable, but using
serializable won't do what I need. I have a java program that must
talk to a box and I don't have access to the sw on this box. I need
to send a datagram with a C structure in it to the box to control it.
The C structure is about 15 or so shorts and ints. I'm converting the
C to Java and need to send the same data format from a java program.
I've defined a Java Class consisting of the ints and shorts in the
right order.

Well, not really in the "right order", since the order of the fields of a
Java class is not defined.
Now I want to stick it in a DatagramPacket and send it.
Problem is all the Java serializable capabilities stick other junk
like class id into the output stream. How do I do it and JUST SEND
THE DATA, i.e. the ints and shorts like any old school C or C++
programmer would do?

Write a method that does it, e.g.

class Datagram {
int i;
short s;
...

void write(DataOutputStream os) {
os.writeInt(i);
os.writeShort(s);
...
}
}

Note that integer types are always written most significant byte first.

Alternatively, look at java.nio, in particular ByteBuffer and Channel.
 
S

Steven Line

How do I get the bytes from an int? I'd prefer to build a buffer of
bytes then send the buffer in one call instead of calling some
streamwrite function on each int and short. That sounds to me like it
would introduce delays between the fields. besides i need to stick it in
a datagram packet first.

Thanks for your suggestions.

Steve
 
S

Steven Line

Looks like ByteBuffer will do the job. Good obscure class recommendation!
Thanks,
Steve
 
T

Tony Morris

How do I get the bytes from an int?
You don't, DataOutputStream does it.

I'd prefer to build a buffer of
bytes then send the buffer in one call instead of calling some
streamwrite function on each int and short.
So, write a method that does it. Serilization doesn't do it anyway - it's
Java's way of representing an object in bytes and it's not just data members
of the object (Try It And See(TM)).

That sounds to me like it
would introduce delays between the fields.
No, it won't. The bottleneck is the network in this case, not the VM.

besides i need to stick it in
a datagram packet first.
If you are using UDP, are you aware that you aren't guaranteed delivery ?
What is the application ?


--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
M

Michael Borgwardt

Tony said:
No, it won't. The bottleneck is the network in this case, not the VM.

I think he means network delays. Sending each field in a separate IP packet would be
stupid (and could happen). But that's what we have BufferedOutputStream for.
 
X

xarax

Steven Line said:
Looks like ByteBuffer will do the job. Good obscure class recommendation!
Thanks,
Steve

The ByteBuffer and Channel things are definitely the
correct way to do this. Be sure to use the BigEndian
versus LittleEndian thing in the ByteBuffer when you
write the short and int values, so they end up in the
platform dependent byte order on the C side.

I went a step farther by writing a program generator
that takes a specification of the fields/types and
generates the Java and C source files that can handle
the message packets in both directions. It generates
the buffer structs and C wrapper getter/setter functions
for each named field, as well as the Java wrapper classes
for each message packet type. It's a simple matter to call
the getter/setter methods to pull/push the field values by
name, and to send or receive the message packet through
the Channel i/o thing. Each message type on the Java side
is instantiated as a Java class. A similar thing happens
on the C side using function pointers as a substitute for
instance methods.

Way too easy. ;)
 
T

Tony Morris

right, perhaps I misunderstood the reasoning.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top