Communication between a Java and a C application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • golabi
    New Member
    • Sep 2009
    • 4

    Communication between a Java and a C application

    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
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by golabi
    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

    Comment

    • golabi
      New Member
      • Sep 2009
      • 4

      #3
      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?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by golabi
        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

        Comment

        • golabi
          New Member
          • Sep 2009
          • 4

          #5
          Thanks so much for the elaborated answer

          Comment

          • golabi
            New Member
            • Sep 2009
            • 4

            #6
            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????

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              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

              Comment

              Working...