static void Main(string[] args) { // Create the NetMQ context using (var context = NetMQContext.Create()) { // Setup a default message factory var messageFactory = new MessageFactory(); // Setup a message handler and register a request handler var messageHandler = RequestDispatcher.Create() .Register<RandomNumberRequest, RandomNumberResponse>(new RandomNumberRequestHandler()); // Create the server and client Server server = new Server(new NetMQReceiverManager(context, "tcp://127.0.0.1:5556"), messageFactory, messageHandler, "ExampleServer"); Client client = new Client(new NetMQSenderManager(context, "tcp://127.0.0.1:5556"), messageFactory); // Run the server in a task with a cancellation token to cancel the task later var cancellationTokenSource = new CancellationTokenSource(); Task.Run(() => server.Run(cancellationTokenSource.Token)); // Send 10 requests to the server and display results for (int i = 0; i < 10; i++) { var request = new RandomNumberRequest() { Min = i, Max = 10 * i * i }; var response = client.Send<RandomNumberResponse, RandomNumberRequest>(request); Console.WriteLine("Request random number betweem {0} to {1}", request.Min, request.Max); Console.WriteLine("Response: {0}\n", response.RandomNumber); } // Stop the server cancellationTokenSource.Cancel(); } }
static void Main(string[] args) { // Create the NetMQ context using (var context = NetMQContext.Create()) { // Setup a default message factory var messageFactory = new MessageFactory(); // Setup a message handler and register two request handlers var messageHandler = RequestDispatcher.Create() .Register<EchoRequest, EchoResponse>(new EchoRequestHandler()) .Register<QuadraticRequest, QuadraticResponse>(new QuadraticRequestHandler()); // Create the server and client Server server = new Server(new NetMQReceiverManager(context, "tcp://127.0.0.1:5556"), messageFactory, messageHandler, "ExampleServer"); Client client = new Client(new NetMQSenderManager(context, "tcp://127.0.0.1:5556"), messageFactory); // Run the server in a task with a cancellation token to cancel the task later var cancellationTokenSource = new CancellationTokenSource(); Task.Run(() => server.Run(cancellationTokenSource.Token)); // Make some requests from the server var echo1 = new EchoRequest() { Name = "Jon", Message = "My first message!" }; var echo2 = client.Send<EchoResponse, EchoRequest>(echo1); Display(echo1); Display(echo2); var echo3 = new EchoRequest() { Name = "Steve", Message = "Hi, I am steve" }; var echo4 = client.Send<EchoResponse, EchoRequest>(echo3); Display(echo3); Display(echo4); var quad1 = new QuadraticRequest() { A = 1, B = 2, C = 3 }; var quad2 = client.Send<QuadraticResponse, QuadraticRequest>(quad1); Display(quad1); Display(quad2); var quad3 = new QuadraticRequest() { A = -1, B = 5, C = 4 }; var quad4 = client.Send<QuadraticResponse, QuadraticRequest>(quad3); Display(quad3); Display(quad4); // Stop the server cancellationTokenSource.Cancel(); } }