Connecting Tech Pros Worldwide Help | Site Map

Communication between a Java and a C application

Newbie
 
Join Date: Sep 2009
Posts: 4
#1: Sep 9 '09
Hi

I have a java application that runs a C application native code(exe file) as a proccess. I need to communicate with this C application and stop it's method, get a data out of it then change it in java application and send back the new data to the C application to continue the proccess. I am thinking about pipe or socket.
Fist I dont know how to comunicate to a exe file and Second what is the most simple and fast way to do it?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Sep 9 '09

re: Communication between a Java and a C application


Quote:

Originally Posted by golabi View Post

I have a java application that runs a C application native code(exe file) as a proccess. I need to communicate with this C application and stop it's method, get a data out of it then change it in java application and send back the new data to the C application to continue the proccess. I am thinking about pipe or socket.
Fist I dont know how to comunicate to a exe file and Second what is the most simple and fast way to do it?

If that C application doesn't open a communication socket you can forget that alternative; you have to communicate through ordinary input and output streams, in particular stdin and stdout (in C terminology). Read the API documentation of the Process class.

If that C application doesn't even do that you have to forget any form of communication between the two applications.

kind regards,

Jos
Newbie
 
Join Date: Sep 2009
Posts: 4
#3: Sep 9 '09

re: Communication between a Java and a C application


Thanks Jos for your quick respond

Actually I am quite new in C . Actually I have the C application source code and I should change it to be able to communicate. Is it right to make an exe file after changing C application to be able to open a socket or ordinary input output stream,
and then use this exe file in java application?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Sep 9 '09

re: Communication between a Java and a C application


Quote:

Originally Posted by golabi View Post

Thanks Jos for your quick respond

Actually I am quite new in C . Actually I have the C application source code and I should change it to be able to communicate. Is it right to make an exe file after changing C application to be able to open a socket or ordinary input output stream,
and then use this exe file in java application?

Yep, if you can change the C source code you can make it open a socket for communication. The Java application would send commands and receive the results through that socket. If can even make that C application quit by issuing the correct command. Your C application turns into a small server so to speak and your Java application will be its client.

Otherwise, if you hate sockets or are allergic to them ;-) you can issue commands through the C application's input stream (stdin) and it can send the results through its output stream (stdout). The Java application can write to C's input stream and read from its output stream. Again, read the API documentation for the Process class.

kind regards,

Jos
Newbie
 
Join Date: Sep 2009
Posts: 4
#5: Sep 9 '09

re: Communication between a Java and a C application


Thanks so much for the elaborated answer
Newbie
 
Join Date: Sep 2009
Posts: 4
#6: Sep 11 '09

re: Communication between a Java and a C application


I will appreciate if anyone answers my question because I still have problems. I decided to use stdin and stdout. I created a child process in java to run the C application and using getInputStream and getoutputStream in 2 different threads but it hangs maybe it because both treads are working with the same files and if I synchronize it dead lock will happen.
So I thought I should write the output of C into a file and read it by java then change it and send it back in the same way. But still I dont know How can I stop the running C process to listen to the java and get its input from the java application????
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Sep 11 '09

re: Communication between a Java and a C application


There are quite some pitfalls with running an external process you have to be aware of; read this article; it explains how to circumvent them.

kind regards,

Jos
Reply