There are many ways to do inter process communication,
but if you are looking for a quick and easy solution you may want to look at ZeroMQ.
WCF is also an option but it might be overkill in your situation.
You can find more information about ZeroMQ here: http://www.zeromq.org/
And you can install it into your project using NuGet.
A quick example with a server
and a client
:
The server listens for connections, expects a string, reverses the string and returns it:
public class Server
{
public Server()
{
}
public void Listen()
{
Task.Run(() =>
{
using (var context = new Context())
{
//Open a socket to reply
using (var socket = context.Socket(SocketType.REP))
{
socket.Bind("tcp://127.0.0.1:32500");
while (true)
{
//Again you could also receive binary data if you want
var request = socket.Recv(Encoding.UTF8);
var response = ReverseString(request);
socket.Send(response, Encoding.UTF8);
}
}
}
});
}
private string ReverseString(string request)
{
var chars = request.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
}
The client connects to the server (in this case the same machine):
public class Client
{
public Client()
{
}
public string ReverseString(string message)
{
using (var context = new Context())
{
//Open a socket to request data
using (var socket = context.Socket(SocketType.REQ))
{
socket.Connect("tcp://127.0.0.1:32500");
//Send a string, you can send a byte[] as well, for example protobuf encoded data
socket.Send(message, Encoding.UTF8);
//Get the response from the server
return socket.Recv(Encoding.UTF8);
}
}
}
}
To test it, the program might look like this:
public class Program
{
public static void Main()
{
new Program();
}
public Program()
{
var server = new Server();
server.Listen();
var client = new Client();
var input = String.Empty;
while (input != "/quit")
{
input = Console.ReadLine();
Console.WriteLine(client.ReverseString(input));
}
}
}
It's easy and it gets the job done.
Another alternative is to use named pipes for IPC: http://www.codeproject.com/Tips/492231/Csharp-Async-Named-Pipes