Skip to main content
gamedev.net
🔒 Locked

Interfacing C program with Java Program

Started by shaddowofadream May 25, 2003 at 1:42 PM 5 replies 1.1k views
Original Post
shaddowofadream
shaddowofadream
hey Im sure that this issue has come up before a programmer before ... but im not sure howto takle this problem so im asking for help from the community... I need to write some software in C/C++, to do some stuff... once it has done its "stuff" i need it to send a message/text/binary... to a java applet which interprets the message .. re-assembles it to the original form and then lets the user play with it.... our project team were originally going to go with straight c program however we we''re told that the end user part needs to run on a web based situation (ie java)... ive tried some simple mock up programs that write a few variables to a binary file then try and read from same file in the other language to no avail ... i really dont want to have to send the msgs as txt, as I know write/read operations on txt files are really slow... any ideas or websites that could help would be greatly appreciated
"... We live on a placid island of ignorance in the midst of black seas of infinity, and it was not meant that we should voyage far." - H.P. Lovecraft The Call to Cthulu
zaniwhoop2
zaniwhoop2
JNI

You can buy the book, or view it online as HTML or PDF

[edited by - zaniwhoop2 on May 25, 2003 3:09:30 PM]
shaddowofadream
shaddowofadream
hrm... that link you posted seems to describe howto incorporate C++ code so that it is run as a java (applet/application)

however what im trying to make is a C++ program that generates a file

and a java file that gets this file and re-constructs it into the original message...

(its 3am btw so maybe Ive overlooked something in the JNI manual)
"... We live on a placid island of ignorance in the midst of black seas of infinity, and it was not meant that we should voyage far." - H.P. Lovecraft The Call to Cthulu
cwraith
cwraith
No, you didn''t overlook anything. JNI isn''t appropriate at all for this.

Transferring data between java and C isn''t trivial, as it actually requires that you specify the transfer format well. You can''t just use the C/C++ hack of writing a struct or a class to the output stream directly, and count on the fact that both sides used the same compiler to get decoding correct. You can''t even use java''s much better specified serialization system, because it''s not implemented in C (and can''t be, in general.)

So you''ve got to specify a format. Text or binary doesn''t matter. Either way, you have several things you need to specify.

For binary, big-endian or little-endian? For variable length fields, how do you specify the end of the field? For floating point numbers, what binary format (IEE 754 or something else)?

For text, what text encoding (UTF-8, cp1252, etc)? What line ending?

Once you have an exact specification of the format, writing a routine to output it in one language, and read it in another should be relatively easy.
mond
mond
What about using XML as your transport mechanism? I''ve used it several times when I need to send data between different application types.
cmptrgear
cmptrgear
Actually, if you know any network programming you could use sockets to transfer the information between the two programs. I dont know what the setup of your project is and I have never tried it myself but it seems like the simplest way to get two different programming languages to pass information back and forth.

"I may not agree with what you say but I will defend to the death your right to say it."
--Voltaire
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.