communication between Java and C++

Discussion in 'Java' started by Markus, Oct 22, 2003.

  1. Markus

    Markus Guest

    Hi!

    I came across the following problem I have to solve. The operating
    system is WinXP Pro.

    There are two programs, a C++- and a Java-program. The two programs
    have to exchange data. I was looking for an effective way to do that,
    and finally I chose a file as "shared resource". Both, C++ and Java
    must read/write into the shared resource. However, the file must not
    be accessed simultaneously by both of them. Additionally there could
    be several instances of the C++-progamm.

    1. The C++-Side
    I use the CFile-Class from MFC with Visual C++. The CFile-Class
    enables to open a file in exclusive-mode as following:

    CFile MyFile;
    MyFile.Open("filename", CFile::modeWrite | CFile::shareExclusive);

    Using Random File Access I read and write the following type of struct
    in/from the file:

    struct t_dataset {
    int flag;
    int ID;
    char Telegramm[29];
    };

    Ok, up to now it works pretty well so far.

    2. The Java-Side
    Browsing and searching the Java-Docs I found out that there is a
    "FileLock"-class which enables to lock a file in terms of the used
    operating system. The mechanism is for instance described here:
    http://developer.java.sun.com/developer/JDCTechTips/2002/tt0924.html

    Here are my questions:
    - Is there a much more effective way to exchange data between my C++-
    and Java-programmes on a windows-platform? If so which one do you
    suggest and why is it better than files?
    - How can I implement the file-access in Java? Assumed I use the
    "RandomAccessFile"-class, there are just methods like this:

    +int read(byte[] b, int off, int len)
    +void write(byte[] b, int off, int len)

    So how can I handle the data defined in the C++-struct above? As far
    as I know I would have to define a class like

    class t_dataset {
    int flag;
    int ID;
    ...
    }

    But I think that's not a good solution.
    Do I have to read a byte-stream and afterwards "extract" and cast the
    data?

    Thank you,
    Markus
    Markus, Oct 22, 2003
    #1
    1. Advertising

  2. Markus wrote:
    > - Is there a much more effective way to exchange data between my C++-
    > and Java-programmes on a windows-platform? If so which one do you
    > suggest and why is it better than files?


    Socket communciation is the standard method for communication between
    programs. File-based IPC is a godawful can of worms.


    > - How can I implement the file-access in Java? Assumed I use the


    I'll answer this because the same questions arise with socket
    communication.

    > "RandomAccessFile"-class, there are just methods like this:
    >
    > +int read(byte[] b, int off, int len)
    > +void write(byte[] b, int off, int len)


    Nope. There are also methods like
    readInt() and readChar().

    > So how can I handle the data defined in the C++-struct above? As far
    > as I know I would have to define a class like
    >
    > class t_dataset {
    > int flag;
    > int ID;
    > ...
    > }
    >
    > But I think that's not a good solution.


    Actually, it is, for representing the data. It does not in any way help you read it
    from the file

    > Do I have to read a byte-stream and afterwards "extract" and cast the
    > data?


    Yup. But that's very easy, with the methods defined in DataInput (which is implemented
    by both RandomAccessFile and DataInputStream. Just be aware that Java exclusively uses
    big-endian signed integers.
    Michael Borgwardt, Oct 22, 2003
    #2
    1. Advertising

  3. Have you thought of Corba???

    -Frederic

    "Markus" <> wrote in message
    news:...
    > Hi!
    >
    > I came across the following problem I have to solve. The operating
    > system is WinXP Pro.
    >
    > There are two programs, a C++- and a Java-program. The two programs
    > have to exchange data. I was looking for an effective way to do that,
    > and finally I chose a file as "shared resource". Both, C++ and Java
    > must read/write into the shared resource. However, the file must not
    > be accessed simultaneously by both of them. Additionally there could
    > be several instances of the C++-progamm.
    >
    > 1. The C++-Side
    > I use the CFile-Class from MFC with Visual C++. The CFile-Class
    > enables to open a file in exclusive-mode as following:
    >
    > CFile MyFile;
    > MyFile.Open("filename", CFile::modeWrite | CFile::shareExclusive);
    >
    > Using Random File Access I read and write the following type of struct
    > in/from the file:
    >
    > struct t_dataset {
    > int flag;
    > int ID;
    > char Telegramm[29];
    > };
    >
    > Ok, up to now it works pretty well so far.
    >
    > 2. The Java-Side
    > Browsing and searching the Java-Docs I found out that there is a
    > "FileLock"-class which enables to lock a file in terms of the used
    > operating system. The mechanism is for instance described here:
    > http://developer.java.sun.com/developer/JDCTechTips/2002/tt0924.html
    >
    > Here are my questions:
    > - Is there a much more effective way to exchange data between my C++-
    > and Java-programmes on a windows-platform? If so which one do you
    > suggest and why is it better than files?
    > - How can I implement the file-access in Java? Assumed I use the
    > "RandomAccessFile"-class, there are just methods like this:
    >
    > +int read(byte[] b, int off, int len)
    > +void write(byte[] b, int off, int len)
    >
    > So how can I handle the data defined in the C++-struct above? As far
    > as I know I would have to define a class like
    >
    > class t_dataset {
    > int flag;
    > int ID;
    > ...
    > }
    >
    > But I think that's not a good solution.
    > Do I have to read a byte-stream and afterwards "extract" and cast the
    > data?
    >
    > Thank you,
    > Markus
    Frederic Pepin, Oct 22, 2003
    #3
  4. Markus

    Phil... Guest

    Would this work: Shared memory?

    "Michael Borgwardt" <> wrote in message
    news:bn68al$tb7va$-berlin.de...
    > Markus wrote:
    > > - Is there a much more effective way to exchange data between my C++-
    > > and Java-programmes on a windows-platform? If so which one do you
    > > suggest and why is it better than files?

    >
    > Socket communciation is the standard method for communication between
    > programs. File-based IPC is a godawful can of worms.
    >
    >
    > > - How can I implement the file-access in Java? Assumed I use the

    >
    > I'll answer this because the same questions arise with socket
    > communication.
    >
    > > "RandomAccessFile"-class, there are just methods like this:
    > >
    > > +int read(byte[] b, int off, int len)
    > > +void write(byte[] b, int off, int len)

    >
    > Nope. There are also methods like
    > readInt() and readChar().
    >
    > > So how can I handle the data defined in the C++-struct above? As far
    > > as I know I would have to define a class like
    > >
    > > class t_dataset {
    > > int flag;
    > > int ID;
    > > ...
    > > }
    > >
    > > But I think that's not a good solution.

    >
    > Actually, it is, for representing the data. It does not in any way help

    you read it
    > from the file
    >
    > > Do I have to read a byte-stream and afterwards "extract" and cast the
    > > data?

    >
    > Yup. But that's very easy, with the methods defined in DataInput (which is

    implemented
    > by both RandomAccessFile and DataInputStream. Just be aware that Java

    exclusively uses
    > big-endian signed integers.
    >
    Phil..., Oct 22, 2003
    #4
  5. Phil... wrote:
    > Would this work: Shared memory?


    Nope, not supported by Java, and an even bigger can of worms.
    Michael Borgwardt, Oct 22, 2003
    #5
  6. Michael Borgwardt <> writes:

    > Nope, not supported by Java, and an even bigger can of worms.


    Well, if the "memory" is a region of a file on disk you can use
    java.nio.channels.FileChannel with java.nio.MappedByteBuffer.
    Tor Iver Wilhelmsen, Oct 22, 2003
    #6
  7. Markus

    Joe Guest

    In article <>,
    says...

    > Here are my questions:
    > - Is there a much more effective way to exchange data between my C++-
    > and Java-programmes on a windows-platform? If so which one do you
    > suggest and why is it better than files?
    > - How can I implement the file-access in Java? Assumed I use the
    > "RandomAccessFile"-class, there are just methods like this:



    xml-rpc
    It's simple, stable, works cross-platform and is more easily debugged.





    --
    "Everybody plays the fool. Sometimes."
    -- American Folk Saying
    Joe, Oct 23, 2003
    #7
  8. On 22 Oct 2003 08:25:09 -0700, (Markus) wrote:

    >Hi!
    >
    >I came across the following problem I have to solve. The operating
    >system is WinXP Pro.
    >
    >There are two programs, a C++- and a Java-program. The two programs
    >have to exchange data. I was looking for an effective way to do that,
    >and finally I chose a file as "shared resource". Both, C++ and Java
    >must read/write into the shared resource. However, the file must not
    >be accessed simultaneously by both of them. Additionally there could
    >be several instances of the C++-progamm.
    >
    >1. The C++-Side
    >I use the CFile-Class from MFC with Visual C++. The CFile-Class
    >enables to open a file in exclusive-mode as following:
    >
    >CFile MyFile;
    >MyFile.Open("filename", CFile::modeWrite | CFile::shareExclusive);
    >
    >Using Random File Access I read and write the following type of struct
    >in/from the file:
    >
    >struct t_dataset {
    > int flag;
    > int ID;
    > char Telegramm[29];
    >};
    >
    >Ok, up to now it works pretty well so far.
    >
    >2. The Java-Side
    >Browsing and searching the Java-Docs I found out that there is a
    >"FileLock"-class which enables to lock a file in terms of the used
    >operating system. The mechanism is for instance described here:
    >http://developer.java.sun.com/developer/JDCTechTips/2002/tt0924.html
    >
    >Here are my questions:
    >- Is there a much more effective way to exchange data between my C++-
    >and Java-programmes on a windows-platform? If so which one do you
    >suggest and why is it better than files?
    >- How can I implement the file-access in Java? Assumed I use the
    >"RandomAccessFile"-class, there are just methods like this:
    >
    >+int read(byte[] b, int off, int len)
    >+void write(byte[] b, int off, int len)
    >
    >So how can I handle the data defined in the C++-struct above? As far
    >as I know I would have to define a class like
    >
    >class t_dataset {
    > int flag;
    > int ID;
    > ...
    >}
    >
    >But I think that's not a good solution.
    >Do I have to read a byte-stream and afterwards "extract" and cast the
    >data?


    Essentially, yes.

    Why don't you use a mailslot instead of a physical file?

    You can probably access it as a file (pseudo-file) from Java, and if not,
    you make a small JNI DLL that you call from Java.
    Alf P. Steinbach, Oct 23, 2003
    #8
  9. Markus

    Chris Uppal Guest

    Markus wrote:

    > There are two programs, a C++- and a Java-program. The two programs
    > have to exchange data. I was looking for an effective way to do that,
    > and finally I chose a file as "shared resource". Both, C++ and Java
    > must read/write into the shared resource. However, the file must not
    > be accessed simultaneously by both of them. Additionally there could
    > be several instances of the C++-progamm.


    I wouldn't normally advocate JNI except as a measure of last resort, but in
    this case it seems like quite a good fit.

    Since you are going to have to solve the problem of multiple concurent access
    to the shared data (however you implement it) from your C++ program anyway, why
    not wrap that solution up as a little library and use it (via JNI) from Java
    too ?

    Why write the same code twice ?

    -- chris
    Chris Uppal, Oct 23, 2003
    #9
  10. Markus

    Markus Guest

    "Frederic Pepin" <> wrote in message news:<jJxlb.5752$>...
    > Have you thought of Corba???


    Yes, I've thought of Corba. However I think using CORBA here is
    something like breaking a butterfly on a wheel. Additionally I've only
    few experience with CORBA so far. So I guess that I'll implement it as
    a shared-file-ressource.
    Markus, Oct 23, 2003
    #10
  11. Markus

    Markus Guest

    Chris Uppal wrote:
    >
    > I wouldn't normally advocate JNI except as a measure of last resort, but in
    > this case it seems like quite a good fit.
    >
    > Since you are going to have to solve the problem of multiple concurent access
    > to the shared data (however you implement it) from your C++ program anyway,why
    > not wrap that solution up as a little library and use it (via JNI) from Java
    > too ?
    >
    > Why write the same code twice ?


    I thought of JNI and decided not to use it because of a important
    C-library I use in the C++-program. The reason is that this lib
    operates very close to the hardware and building a working wrapper
    around it could be a tricky effort. Another reason is that the vendor
    (Siemens) of this lib discourages from using it within different
    threads in the same C/C++-program. And that's what I would have to do
    except I'm trying to use JNI and i.e. JMS between the resulting
    Java-programs.
    Consequently I decided to develop to types of programs: Type1 (C++)
    which could exist n-times, and Type2 (Java) from which there's exactly
    one instance running.
    The Type1-program will be explicitly started by the user for every new
    hardware-device.

    I agree that the code dealing with reading/writing/locking the file
    must be implemented in C++ as well as in Java. However I think that's
    better than dealing with JNI/Multithreading and finally find out that
    it didn't work.

    -- Markus --
    Markus, Oct 24, 2003
    #11
  12. Markus wrote:

    > - Is there a much more effective way to exchange data between my C++-
    > and Java-programmes on a windows-platform? If so which one do you
    > suggest and why is it better than files?


    Check MsgConnect (http://www.msgconnect.com/). It lets you easily
    exchange data messages between various platforms without the need to
    deal with sockets too much.

    --
    Eugene Mayevski
    EldoS Corp., CTO
    Networking and security solutions, custom development services
    http://www.eldos.com
    Eugene Mayevski, Oct 31, 2003
    #12
  13. Markus

    Roedy Green Guest

    On Fri, 31 Oct 2003 18:13:23 +0200, Eugene Mayevski
    <> wrote or quoted :

    >> - Is there a much more effective way to exchange data between my C++-
    >> and Java-programmes on a windows-platform? If so which one do you
    >> suggest and why is it better than files?


    the traditional choices are:
    exec
    JNI
    sockets

    see http://mindprod.com/jgloss/jni.html
    for a discussion of the relative merits.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Oct 31, 2003
    #13
  14. Markus

    nos Guest

    is there some trick to get the equivalent of shared memory?
    i'm thinking about creating a big StringBuffer in java and
    passing the reference back and forth to your 'c' programs


    "Roedy Green" <> wrote in message
    news:...
    > On Fri, 31 Oct 2003 18:13:23 +0200, Eugene Mayevski
    > <> wrote or quoted :
    >
    > >> - Is there a much more effective way to exchange data between my C++-
    > >> and Java-programmes on a windows-platform? If so which one do you
    > >> suggest and why is it better than files?

    >
    > the traditional choices are:
    > exec
    > JNI
    > sockets
    >
    > see http://mindprod.com/jgloss/jni.html
    > for a discussion of the relative merits.
    >
    > --
    > Canadian Mind Products, Roedy Green.
    > Coaching, problem solving, economical contract programming.
    > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    nos, Oct 31, 2003
    #14
  15. Markus

    Sudsy Guest

    nos wrote:
    > is there some trick to get the equivalent of shared memory?
    > i'm thinking about creating a big StringBuffer in java and
    > passing the reference back and forth to your 'c' programs


    But then you'd also need semaphores to control access to the
    shared memory segment...
    Wait a moment! Why not just call synchronized Java methods
    from the JNI code?!
    "There's more than one way to skin a cat" is an old adage
    and quite applicable here. While you might be familiar and
    comfortable with shared memory in the C world, you might
    want to use the most appropriate tool for the job at hand.
    Trying to shoehorn a shared memory solution into the Java
    application sphere might be ill-considered.
    "A place for everything and everything in its place".
    Sudsy, Nov 1, 2003
    #15
  16. Markus

    jp Guest

    i use sockets, a 20 byte message header indicating the type and size of
    the message, & a message protocol.

    the protocol is implimented as a state machine in complimentary classes
    in Java & C++. if a process expects to read a socket, it blocks or
    times out & disconnects.

    message contents can be binary or ascii.
    ascii contents can be fields/records, lisp or xml.

    i like sockets cos theyre fast thru localhost, but you can still use
    them over a network. also, my code isnt generally win32 specific.


    any help?



    Eugene Mayevski wrote:
    > Markus wrote:
    >
    >> - Is there a much more effective way to exchange data between my C++-
    >> and Java-programmes on a windows-platform? If so which one do you
    >> suggest and why is it better than files?

    >
    >
    > Check MsgConnect (http://www.msgconnect.com/). It lets you easily
    > exchange data messages between various platforms without the need to
    > deal with sockets too much.
    >
    jp, Nov 6, 2003
    #16
  17. Markus

    Markus Guest

    Sounds very nice...

    Would you mind to send me the source-code?

    Tnx,
    Markus
    Markus, Nov 7, 2003
    #17
  18. What about pipes? Do pipes work for Java? It would be a nice easy
    solution I think... Check it out :)
    Chris Mantoulidis, Nov 7, 2003
    #18
  19. Markus

    nos Guest

    you have to smoke outside


    "Chris Mantoulidis" <> wrote in message
    news:...
    > What about pipes? Do pipes work for Java? It would be a nice easy
    > solution I think... Check it out :)
    nos, Nov 7, 2003
    #19
    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. szymex
    Replies:
    1
    Views:
    413
    Michael Borgwardt
    May 3, 2004
  2. Tyler
    Replies:
    6
    Views:
    8,782
    sukahati
    May 13, 2008
  3. Jacques Daussy

    Communication between JAVA and python

    Jacques Daussy, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    373
    Ulrich Schaefer
    Feb 24, 2005
  4. thiagu_hi
    Replies:
    0
    Views:
    431
    thiagu_hi
    Jan 29, 2008
  5. thiagu_hi
    Replies:
    0
    Views:
    425
    thiagu_hi
    Jan 29, 2008
Loading...

Share This Page