3

I have a program written in C# that receives data from a third party server and processes it into a series of integers (they are streaming rapidly). The program is written in C# because the third party provides classes to process the data, but only offers them in C#. I would like to take these integers and use them in a streaming way in a Java program (so as soon as they are streamed and processed by the C# program, I would like to use the integers in the Java program). The second program is in Java because another third party only offers their classes (which are required) in Java. So my guess for what to do is either-

  1. Look for a program that runs C# classes in Java, and then include the C# classes that process the incoming data directly into my Java program [This doesn't seem too promising - I can't seem to get jni4net, which was suggested in other posts, to work]

or

  1. Write another program in C# that saves to a particular memory location the integers that are being processed. Compile the program. Then run the executable from within the Java source code and have some sort of callback written in the Java code that picks up when the integers at the specific memory locations are changed and record what these new numbers are [Not sure how to start on this!]

Does anyone have any suggestions for what might be the least painful approach?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Chris W
  • 145
  • 1
  • 2
  • 12
  • 1
    Check this out: http://stackoverflow.com/questions/14824491/can-i-communicate-between-java-and-c-sharp-using-just-sockets – Conrad Clark Apr 05 '13 at 16:55
  • Why do you need to do this? Is there some sort of requirement outside of your control forcing you to use 2 different languages, or do both applications more or less already exist? – Sam I am says Reinstate Monica Apr 05 '13 at 16:55
  • 1
    Just create a named pipe between the java and C# applications. Sounds like the most appropriate in your case. In the general case just look up "inter process communication". There are lots of language-independent ways of communication between programs. – Servy Apr 05 '13 at 16:55
  • 1
    @ConradClark Sockets are probably not the simplest method, although it's one possible option. – Servy Apr 05 '13 at 16:56
  • Sam - I have no choice on using the two languages unfortunately, I'm trying to process data that requires compiled classes exclusively in Java and exclusively in C#. Thanks for the responses you guys - It might take some time to go through your suggestions because I'm not familiar with IPC, but I'll report back once I find a solution that works! – Chris W Apr 05 '13 at 17:13

3 Answers3

3

It sounds like you want some form of inter-process communication mechanism.

As such, anything allowing communication could be employed:

  1. Named Pipes
  2. Sockets
  3. TCP connection
  4. Shared memory

Out of the options I would recommend named pipes as they're the simplest to grasp and have no problems with ports not being available etc.

See: How to open a Windows named pipe from Java?

And: Using Named Pipes for IPC in C#

This post: Using Named Pipes to communicate between C# and Java describes and end-to-end means of using pipes from both environments.

Community
  • 1
  • 1
Clint
  • 6,133
  • 2
  • 27
  • 48
0

You can merge the Java and C# programs into one and use JNI to bridge the C#/Java interface. JNI basically allows you to call C# methods from Java and vice versa. I guess this would be equally hard to code as sockets, plus there would be some speedup.

More info here: http://www.codeproject.com/Articles/245622/Using-the-Java-Native-Interface-in-Csharp

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
  • Not only would this be harder than some of the other methods, but I don't think it's good design. Here we have two logically separate programs that need to communicate. Rather than having them know about each other and intimately couple them together in this way they can each communicate through the OS through some IPC method, reducing coupling. – Servy Apr 05 '13 at 17:06
0

I had to do something similar in the past in order to get data from a Java system into Excel (via the RTD API), and ended up going with the socket protocol approach.

If you have a really simple dataset (like the stream of integers you mention above), this should be very straighforward - just look at the documentation for the Java Socket and ServerSocket classes, and the corresponding C# Socket class.

If you end up with a more complex API, with multiple messages etc. you might want to take a look at Google Protocol Buffers, as there are both .Net and Java implementations around.

steve cook
  • 3,116
  • 3
  • 30
  • 51