communication between Java and C++

M

Markus

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
 
M

Michael Borgwardt

Markus said:
- 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.
 
P

Phil...

Would this work: Shared memory?

Michael Borgwardt said:
Markus said:
- 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.
 
T

Tor Iver Wilhelmsen

Michael Borgwardt said:
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.
 
J

Joe

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

Alf P. Steinbach

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

Chris Uppal

Markus said:
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
 
M

Markus

Frederic Pepin said:
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.
 
M

Markus

Chris said:
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 --
 
E

Eugene Mayevski

Markus said:
- 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.
 
N

nos

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
 
S

Sudsy

nos said:
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".
 
J

jp

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?
 
C

Chris Mantoulidis

What about pipes? Do pipes work for Java? It would be a nice easy
solution I think... Check it out :)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top