Odys
Odys

Reputation: 9090

Bidirectional IPC between C# applications

I am aware of IPC and have used it in several projects. My new requirements include two simple C# applications that need to exchange short messages like "do this" "done that". This should be bidirectional.

This is going to run on old systems (Win Xp .Net 3.5) without being able to configure them. The end user will just run the application.

Is there a simple way to exchange data between C# applications?

Upvotes: 2

Views: 1384

Answers (2)

Quinton Bernhardt
Quinton Bernhardt

Reputation: 4803

Although you said TCP is out...

i've been very keen to use ZeroMQ but just haven't had an opportunity. It's multi platform with a c# wrapper and is designed exactly for this sort of application messaging. Check out ZeroMQ at http://www.zeromq.org/.

Some sample code from one of their sites:

// Set up a publisher.

var publisher = new ZmqPublishSocket {
    Identity = Guid.NewGuid().ToByteArray(),
    RecoverySeconds = 10
};

publisher.Bind( address: "tcp://127.0.0.1:9292" );

// Set up a subscriber.

var subscriber = new ZmqSubscribeSocket();

subscriber.Connect( address: "tcp://127.0.0.1:9292" );

subscriber.Subscribe( prefix: "" ); // subscribe to all messages

// Add a handler to the subscriber's OnReceive event

subscriber.OnReceive += () => {

    String message;
    subscriber.Receive( out message, nonblocking: true );

    Console.WriteLine( message );
};

// Publish a message to all subscribers.

publisher.Send( "Hello world!" );

Upvotes: 0

JerKimball
JerKimball

Reputation: 16894

You've got a couple options:

  • wcf, which you say you've considered and rejected; may I inquire as to what made you reject it?

  • named pipes, as you mentioned; it's actually reasonably trivial to wrap two named pipes into a bidirectional messaging system (pro tip: make it disposable and clean up the pipes on dispose lest you lock them up)

  • queuing systems, but usually you see these in many-to-many or one-to-many channels

  • serialize a message queue (simple queue structure) and put it in a memory mapped file, then read/write from either side

  • esoteric: use windows messaging, i.e. pinvoke send/post message

Upvotes: 1

Related Questions