communication between Java and C++ - Programmer
This is a discussion on communication between Java and C++ - Programmer ; 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 ...
-
communication between Java and C++
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/develo...02/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
-
Re: communication between Java and C++
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.
-
Re: communication between Java and C++
Have you thought of Corba???
-Frederic
"Markus" wrote in message
news:584006b7.0310220725.24821d00@posting.google.c om...
> 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/develo...02/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
-
Re: communication between Java and C++
Would this work: Shared memory?
"Michael Borgwardt" wrote in message
news:bn68al$tb7va$1@ID-161931.news.uni-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.
>
-
Re: communication between Java and C++
Phil... wrote:
> Would this work: Shared memory?
Nope, not supported by Java, and an even bigger can of worms.
-
Re: communication between Java and C++
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.
-
Re: communication between Java and C++
In article <584006b7.0310220725.24821d00@posting.google.com>,
markus.simmerl@gmx.de 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
-
Re: communication between Java and C++
On 22 Oct 2003 08:25:09 -0700, markus.simmerl@gmx.de (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/develo...02/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.
-
Re: communication between Java and C++
"Frederic Pepin" wrote in message news:...
> 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.
-
Re: communication between Java and C++
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
-
Re: communication between Java and C++
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 --
-
Re: communication between Java and C++
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
-
Re: communication between Java and C++
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.
-
Re: communication between Java and C++
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.
>
-
Re: communication between Java and C++
Sounds very nice...
Would you mind to send me the source-code?
Tnx,
Markus
-
Re: communication between Java and C++
What about pipes? Do pipes work for Java? It would be a nice easy
solution I think... Check it out 
-
Re: communication between Java and C++
you have to smoke outside
"Chris Mantoulidis" wrote in message
news:a8587dd9.0311071059.7de532a6@posting.google.c om...
> What about pipes? Do pipes work for Java? It would be a nice easy
> solution I think... Check it out 