Question on C/C++ and Java TCP/IP

Discussion in 'Java' started by Wonderer, Apr 10, 2004.

  1. Wonderer

    Wonderer Guest

    Hi there!

    we all know that you can send structs between C programs easily with
    TCP/IP, but if u let a C server send a struct to a JAVA client, how will
    the client be able to process the struct?

    Thz
    Wonderer, Apr 10, 2004
    #1
    1. Advertising

  2. Wonderer wrote:

    >
    > we all know that you can send structs between C programs easily with
    > TCP/IP, but if u let a C server send a struct to a JAVA client, how will
    > the client be able to process the struct?


    Was it really necessary to crosspost to all these groups? Somehow I don't
    think this is very relevant to c.l.j.advocacy.

    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Apr 10, 2004
    #2
    1. Advertising

  3. Wonderer wrote:
    > Hi there!
    >
    > we all know that you can send structs between C programs easily with
    > TCP/IP, but if u let a C server send a struct to a JAVA client, how will
    > the client be able to process the struct?
    >
    > Thz


    First you must know exactly what you are sending on the wire.
    You must define this byte-by-byte, exactly as though writing
    a description if a binary file format.

    The trouble is, if you are simply treating your structure
    as a char[] and writing (sizeof MyStruct) bytes as a
    the memory image, then you do not know exactly what you are
    sending. You may need a protocol analyser (or write it to
    file and use a hex editor) to discover where the compiler
    puts padding bytes.

    Once you know what your C code is sending, you can use a
    DataInputStream to read it element by element.

    Steve
    Steve Horsley, Apr 10, 2004
    #3
  4. Wonderer

    Roedy Green Guest

    On Sat, 10 Apr 2004 12:14:37 GMT, "Wonderer" <> wrote or
    quoted :

    >we all know that you can send structs between C programs easily with
    >TCP/IP, but if u let a C server send a struct to a JAVA client, how will
    >the client be able to process the struct?


    Most easily with DataInputStream (or LEDataInputStream if the struct
    in Intel little endian format.)

    See http://mindprod.com/jgloss/endian.html

    You have to know the way your particular C/C++ compiler packs structs.
    Dealing with padding is the most complicated issue.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 10, 2004
    #4
  5. Wonderer

    Chris Smith Guest

    Wonderer wrote:
    > we all know that you can send structs between C programs easily with
    > TCP/IP, but if u let a C server send a struct to a JAVA client, how will
    > the client be able to process the struct?


    Just to clarify things on the C/C++ side. You're probably thinking of
    casting a struct pointer to a char pointer, and then using sizeof to
    determine the size of the struct and sending its in-memory
    representation directly over a TCP socket. You should be aware that
    this works reliably *only* if the following conditions are all
    satisfied:

    1) Both sides are running on the same operating system and hardware.
    2) Both sides are compiled with the same compiler and version.
    3) Both sides are compiled with the same flags/optimization level.

    If you do this, you should find out the exact wire protocol and document
    it, and clearly document that your struct trick works because it happens
    to coincide with the documented wire protocol on a given platform,
    compiler, version, and set of flags. Ideally, you'd document all of
    those details so someone reading could reproduce your protocol. All
    other clients that don't match that platform, compiler, version, or set
    of compile options (including, of course, any Java clients) would need
    to implement I/O over the stream using the documented wire protocol
    rather than the struct trick.

    As for how to do that, it involves DataInputStream and DataOutputStream
    in Java (unless your protocol is little-endian, in which case Roedy's
    web site has a little-endian version of those classes that will do what
    you need). The standard socket routines along the lines of htonl() will
    help in C, but the process is more involved.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Apr 10, 2004
    #5
  6. Chris Smith wrote:
    > Wonderer wrote:
    >
    > As for how to do that, it involves DataInputStream and DataOutputStream
    > in Java (unless your protocol is little-endian, in which case Roedy's
    > web site has a little-endian version of those classes that will do what
    > you need). The standard socket routines along the lines of htonl() will
    > help in C, but the process is more involved.
    >


    The java.nio package is also useful for this type of work, especially
    the java.nio.ByteBuffer class. It supports both little and big endian
    byte orders. However it requires a 1.4 or later JVM.

    Mark Thornton
    Mark Thornton, Apr 10, 2004
    #6
  7. Wonderer

    Raghar Guest

    "Wonderer" <> wrote in
    news:NyRdc.6993$:

    > Hi there!
    >
    > we all know that you can send structs between C programs easily with
    > TCP/IP, but if u let a C server send a struct to a JAVA client, how
    > will the client be able to process the struct?


    Using Java server is highly recommended, or you could prepare youself for
    reinstallation of your favorite OS, or possible breakdowns.
    You shouldn't view it as struct, you should view it as a data and process
    it approperiately.
    Learn asm, and stop thinking on it in C terms. It would highly likely
    work just on one computer. The computer that would have both server and
    client running.
    Raghar, Apr 10, 2004
    #7
  8. Wonderer emitted:

    > Hi there!
    >
    > we all know that you can send structs between C programs easily with
    > TCP/IP, but if u let a C server send a struct to a JAVA client, how will
    > the client be able to process the struct?
    >
    > Thz


    Why it is easily to exchange structs (memory images) between C programms
    over TCP/IP?

    This is only true on the same host and the same alignment and type
    representation and !! no pointers a member of the struct.
    At all other cases it coud be very difficult to interchange complex types
    (structs). It is better to use RPC on C, may be extended to CORBA on C++.

    Regards J.


    --
    Alle in diesem Text gemachte Rechtschreibfehler sind geistiges Eigentum des
    Autors. Wer diese Rechtschreibfehler nachmacht oder verfälscht, kann mit
    Spam nicht unter 1 Megabyte bedacht werden.
    Juergen_Sievers, Apr 10, 2004
    #8
  9. Wonderer

    Liz Guest

    asn.1 ?

    "Mark Thornton" <> wrote in message
    news:8lYdc.15273$4N3.7718@newsfe1-win...
    > Chris Smith wrote:
    > > Wonderer wrote:
    > >
    > > As for how to do that, it involves DataInputStream and DataOutputStream
    > > in Java (unless your protocol is little-endian, in which case Roedy's
    > > web site has a little-endian version of those classes that will do what
    > > you need). The standard socket routines along the lines of htonl() will
    > > help in C, but the process is more involved.
    > >

    >
    > The java.nio package is also useful for this type of work, especially
    > the java.nio.ByteBuffer class. It supports both little and big endian
    > byte orders. However it requires a 1.4 or later JVM.
    >
    > Mark Thornton
    >
    Liz, Apr 10, 2004
    #9
  10. Wonderer

    Liz Guest

    asn.1 ?

    "Mark Thornton" <> wrote in message
    news:8lYdc.15273$4N3.7718@newsfe1-win...
    > Chris Smith wrote:
    > > Wonderer wrote:
    > >
    > > As for how to do that, it involves DataInputStream and DataOutputStream
    > > in Java (unless your protocol is little-endian, in which case Roedy's
    > > web site has a little-endian version of those classes that will do what
    > > you need). The standard socket routines along the lines of htonl() will
    > > help in C, but the process is more involved.
    > >

    >
    > The java.nio package is also useful for this type of work, especially
    > the java.nio.ByteBuffer class. It supports both little and big endian
    > byte orders. However it requires a 1.4 or later JVM.
    >
    > Mark Thornton
    >
    Liz, Apr 10, 2004
    #10
    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. Robert Mark Bram

    Using Java to packetise tcp/ip data?

    Robert Mark Bram, Jan 8, 2004, in forum: Java
    Replies:
    1
    Views:
    424
    Chris Smith
    Jan 8, 2004
  2. bigbinc

    Good tcp java server design

    bigbinc, Jan 20, 2004, in forum: Java
    Replies:
    3
    Views:
    8,791
    bigbinc
    Jan 21, 2004
  3. Replies:
    0
    Views:
    372
  4. Tiger
    Replies:
    5
    Views:
    967
    Dave Thompson
    May 1, 2006
  5. Qu0ll
    Replies:
    10
    Views:
    1,259
    Karl Uppiano
    Oct 6, 2007
Loading...

Share This Page