Sending object over network WITHOUT using serializable interface

Discussion in 'Java' started by Steve Line, Jan 13, 2004.

  1. Steve Line

    Steve Line Guest

    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
    Steve Line, Jan 13, 2004
    #1
    1. Advertising

  2. "Steve Line" <> wrote in message
    news:...
    > 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.
    Mike Schilling, Jan 13, 2004
    #2
    1. Advertising

  3. Steve Line

    Jose Rubio Guest

    Why don't you just write the bytes into the output stream directly?


    "Steve Line" <> wrote in message
    news:...
    > 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
    Jose Rubio, Jan 13, 2004
    #3
  4. Steve Line

    Steven Line Guest

    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

    Jose Rubio wrote:
    > Why don't you just write the bytes into the output stream directly?
    >
    >
    > "Steve Line" <> wrote in message
    > news:...
    >
    >>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

    >
    >
    >
    Steven Line, Jan 13, 2004
    #4
  5. Steve Line

    Steven Line Guest

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

    Mike Schilling wrote:

    > "Steve Line" <> wrote in message
    > news:...
    >
    >>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.
    >
    >
    >
    Steven Line, Jan 13, 2004
    #5
  6. Steve Line

    Tony Morris Guest

    > 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)


    "Steven Line" <> wrote in message
    news:D2KMb.7279$...
    > 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
    >
    > Jose Rubio wrote:
    > > Why don't you just write the bytes into the output stream directly?
    > >
    > >
    > > "Steve Line" <> wrote in message
    > > news:...
    > >
    > >>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

    > >
    > >
    > >

    >
    Tony Morris, Jan 13, 2004
    #6
  7. Tony Morris wrote:

    >> 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.


    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.
    Michael Borgwardt, Jan 13, 2004
    #7
  8. Steve Line

    xarax Guest

    "Steven Line" <> wrote in message
    news:OaKMb.7298$...
    > Looks like ByteBuffer will do the job. Good obscure class recommendation!
    > Thanks,
    > Steve
    >
    > Mike Schilling wrote:
    >
    > > "Steve Line" <> wrote in message
    > > news:...
    > >
    > >>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.


    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. ;)
    xarax, Jan 13, 2004
    #8
  9. Steve Line

    Tony Morris Guest

    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)


    "Michael Borgwardt" <> wrote in message
    news:bu0ett$c4260$-berlin.de...
    > Tony Morris wrote:
    >
    > >> 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.

    >
    > 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.
    >
    Tony Morris, Jan 14, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    833
    placid
    Jan 9, 2006
  2. Thomas Hawtin
    Replies:
    2
    Views:
    6,104
  3. Jimmy
    Replies:
    4
    Views:
    639
    Thomas Hawtin
    Aug 8, 2007
  4. Venkat Sadasivam
    Replies:
    12
    Views:
    589
  5. Replies:
    8
    Views:
    2,963
Loading...

Share This Page