Wednesday, 14 December 2016

zeroMQ: Hello World Application

In this post, I am going to explain simple client-server application.

In summary, client send a string to server, server convert the string to upper case and send back to the client.

Implementing Server Application
Step 1: Get ZContext object.
var context = new ZContext()

Step 2: Create ZSocket of type ‘ZSocketType.REP’. REP stands for reply.
var responder = new ZSocket(context, ZSocketType.REP)

Step 3: Bind the socket to the url.
responder.Bind(url);

‘url’ is of the form protocol://hostname:portNumber.

For Example,
tcp://127.0.0.1:12345

Step 4: By using 'ReceiveFrame' method of the ZSocket, you can read the frame.

ZFrame request = responder.ReceiveFrame()

By using send method of 'ZSocket', you can send the response back to client.

responder.Send(new ZFrame(result));

Implementing Client Application
Step 1: Get ZContext object.
var context = new ZContext()

Step 2: Create ZSocket of type ‘ZSocketType.REQ’. REQ stands for request.
var responder = new ZSocket(context, ZSocketType.REQ)

Step 3: Bind the socket to the url.
responder.Bind(url);

‘url’ is of the form protocol://hostname:portNumber.

For Example,
tcp://127.0.0.1:12345

Step 4: By using 'ReceiveFrame' method of the ZSocket, you can read the frame.

ZFrame request = responder.ReceiveFrame()

By using send method of 'ZSocket', you can send the response back to client.

responder.Send(new ZFrame(result));

Following is the complete working application.

ZeroMQServer.cs
using System;
using ZeroMQ;

namespace zeroMQTutorial
{
    public class ZeroMQServer
    {
        private String protocol;
        private int port;
        private String url;

        public ZeroMQServer(String protocol, int port)
        {
            this.protocol = protocol;
            this.port = port;
            this.url = protocol + "://127.0.0.1:" + port;
        }

        public String processData(String data)
        {
            return data.ToUpper();
        }

        public void startService()
        {
            using (var context = new ZContext())
            using (var responder = new ZSocket(context, ZSocketType.REP))
            {
                responder.Bind(url);

                while (true)
                {
                    using (ZFrame request = responder.ReceiveFrame())
                    {
                        String data = request.ReadString();
                        Console.WriteLine("Received {0} from client", data);

                        String result = processData(data);

                        responder.Send(new ZFrame(result));
                    }
                }
            }
        }
    }
}


ZeroMQClient.cs
using System;
using ZeroMQ;

namespace zeroMQTutorial
{
    class ZeroMQClient
    {
        private String protocol;
        private int port;
        private String url;

        public ZeroMQClient(String protocol, int port)
        {
            this.protocol = protocol;
            this.port = port;
            this.url = protocol + "://127.0.0.1:" + port;
        }

        /* Send data to server and get the response */
        public String sendData(String data)
        {
            using (var context = new ZContext())
            using (var requester = new ZSocket(context, ZSocketType.REQ))
            {
                Console.WriteLine("Sending data {0} to server", data);
                requester.Connect(url);
                requester.Send(new ZFrame(data));

                using (ZFrame reply = requester.ReceiveFrame())
                {
                    String result = reply.ReadString();
                    return result;
                }
            }
        }

    }
}

Program.cs
using System;
using System.Threading;

namespace zeroMQTutorial
{
   class Program
    {

        static void Main(string[] args)
        {
            ZeroMQServer server = new ZeroMQServer("tcp", 12345);
            ZeroMQClient client = new ZeroMQClient("tcp", 12345);

            Thread t1 = new Thread(server.startService);

            t1.Start();

            String result = client.sendData("Hello World");
            Console.WriteLine("result is {0}", result);

        }
    }
}

Run ‘Program.cs’, you can able to see following output.


Output
Sending data Hello World to server
Received Hello World from client
result is HELLO WORLD






Previous                                                 Next                                                 Home

No comments:

Post a Comment