Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CLRZMQ_3
- //#define CLRZMQ_225
- //#define CLRZMQ_2
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- #if(CLRZMQ_225)
- using ZMQ;
- #elif (CLRZMQ_3 || CLRZMQ_2)
- using ZeroMQ;
- #endif
- namespace _2012_Client_64
- {
- class Program
- {
- //Timer check connection
- private static System.Threading.Timer t_NeighConn;
- static void Main(string[] args)
- {
- t_NeighConn = new System.Threading.Timer(neighborsConnection, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5));
- Console.ReadLine();
- }
- private static void neighborsConnection(object state)
- {
- // number of server to call.
- int numServer = 2;
- for (int i = 0; i < numServer; i++)
- {
- ThreadPool.QueueUserWorkItem(ZeroMqCall, i);
- }
- }
- private static void ZeroMqCall(object state)
- {
- Console.WriteLine(state.ToString());
- try
- {
- #if(CLRZMQ_225)
- // ZMQ Context and client socket
- using (Context context = new Context())
- using (Socket client = context.Socket(SocketType.REQ))
- {
- client.Connect("tcp://192.168.1.117:1234");
- string request = "Hello";
- Console.WriteLine("Sending request...");
- client.Send(request, Encoding.UTF8);
- string reply = client.Recv(Encoding.UTF8);
- Console.WriteLine("Received reply!!");
- }
- #elif(CLRZMQ_2)
- // ZMQ Context and client socket
- using (var context = ZmqContext.Create())
- using (ZmqSocket client = context.CreateSocket(SocketType.REQ))
- {
- string _path = "tcp://192.168.3.3:5555";
- client.Connect(_path);
- TimeSpan _timeout = new TimeSpan(0,0,0,2000);
- client.SendTimeout = _timeout;
- client.ReceiveTimeout = _timeout;
- client.Linger = new TimeSpan(0, 0, 0);
- //richiesta
- Console.WriteLine("Sending");
- string _parameter = "to send";
- client.Send(_parameter, Encoding.UTF8, _timeout);
- // risposta (ritorna null se va in timeout)
- string reply = client.Receive(Encoding.UTF8, _timeout);
- if (reply == null)
- client.Disconnect(_path);
- }
- #elif (CLRZMQ_3)
- using (var context = ZmqContext.Create())
- {
- using (ZmqSocket client = context.CreateSocket(SocketType.REQ))
- {
- // No connected server path
- string _path = "tcp://192.168.3.12:5555";
- client.Connect(_path);
- TimeSpan _timeout = new TimeSpan(0, 0, 0, 0, 1000);
- //request
- Console.WriteLine("Sending");
- SocketFlags sf = SocketFlags.DontWait;
- System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
- Byte[] msg = encoding.GetBytes("to send");
- client.Send(msg, msg.Length, sf, _timeout);
- /*
- client.Send("da inviare", Encoding.UTF8, _timeout);
- // risposta (ritorna null se va in timeout)
- string reply = client.Receive(Encoding.UTF8, _timeout);
- Console.WriteLine("Received !!");*/
- byte[] reply = new byte[4];
- client.Receive(reply, _timeout);
- Console.WriteLine("Received !!");
- client.Close();
- }
- }
- #endif
- }
- #if(CLRZMQ_225)
- catch (ZMQ.Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- #elif (CLRZMQ_3 || CLRZMQ_2)
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- #endif
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement