Advertisement
Guest User

C# ZeroMQ no-connected server Request

a guest
May 15th, 2013
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1. #define CLRZMQ_3
  2. //#define CLRZMQ_225
  3. //#define CLRZMQ_2
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Threading;
  11.  
  12. #if(CLRZMQ_225)
  13. using ZMQ;
  14. #elif (CLRZMQ_3 || CLRZMQ_2)
  15. using ZeroMQ;
  16. #endif
  17.  
  18.  
  19. namespace _2012_Client_64
  20. {
  21.     class Program
  22.     {
  23.         //Timer check connection
  24.         private static System.Threading.Timer t_NeighConn;
  25.  
  26.         static void Main(string[] args)
  27.         {
  28.             t_NeighConn = new System.Threading.Timer(neighborsConnection, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5));
  29.  
  30.             Console.ReadLine();
  31.  
  32.          
  33.  
  34.         }
  35.  
  36.         private static void neighborsConnection(object state)
  37.         {
  38.             // number of server to call.
  39.             int numServer = 2;
  40.             for (int i = 0; i < numServer; i++)
  41.             {
  42.                 ThreadPool.QueueUserWorkItem(ZeroMqCall, i);
  43.             }
  44.         }
  45.  
  46.         private static void ZeroMqCall(object state)
  47.         {
  48.             Console.WriteLine(state.ToString());
  49.             try
  50.             {
  51.                  #if(CLRZMQ_225)
  52.             // ZMQ Context and client socket
  53.                 using (Context context = new Context())
  54.                 using (Socket client = context.Socket(SocketType.REQ))
  55.                 {
  56.                     client.Connect("tcp://192.168.1.117:1234");
  57.  
  58.                     string request = "Hello";
  59.  
  60.                       Console.WriteLine("Sending request...");
  61.                       client.Send(request, Encoding.UTF8);
  62.  
  63.                       string reply = client.Recv(Encoding.UTF8);
  64.                       Console.WriteLine("Received reply!!");
  65.  
  66.             }
  67.                 #elif(CLRZMQ_2)
  68.                
  69.             // ZMQ Context and client socket
  70.                 using (var context = ZmqContext.Create())
  71.                 using (ZmqSocket client = context.CreateSocket(SocketType.REQ))
  72.                 {
  73.                     string _path = "tcp://192.168.3.3:5555";
  74.                     client.Connect(_path);
  75.  
  76.                     TimeSpan _timeout = new TimeSpan(0,0,0,2000);
  77.                     client.SendTimeout = _timeout;
  78.                     client.ReceiveTimeout = _timeout;
  79.                     client.Linger = new TimeSpan(0, 0, 0);
  80.  
  81.                     //richiesta
  82.                     Console.WriteLine("Sending");
  83.                     string _parameter = "to send";
  84.                     client.Send(_parameter, Encoding.UTF8, _timeout);
  85.  
  86.                     // risposta (ritorna null se va in timeout)
  87.                     string reply = client.Receive(Encoding.UTF8, _timeout);
  88.  
  89.                     if (reply == null)
  90.                         client.Disconnect(_path);
  91.  
  92.                    
  93.                 }
  94.  
  95.            
  96.             #elif (CLRZMQ_3)
  97.                 using (var context = ZmqContext.Create())
  98.                 {
  99.                     using (ZmqSocket client = context.CreateSocket(SocketType.REQ))
  100.                     {
  101.  
  102.                         // No connected server path
  103.                         string _path = "tcp://192.168.3.12:5555";
  104.                         client.Connect(_path);
  105.  
  106.                         TimeSpan _timeout = new TimeSpan(0, 0, 0, 0, 1000);
  107.  
  108.                         //request
  109.                         Console.WriteLine("Sending");
  110.  
  111.                         SocketFlags sf = SocketFlags.DontWait;
  112.  
  113.                         System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  114.                         Byte[] msg = encoding.GetBytes("to send");
  115.  
  116.                         client.Send(msg, msg.Length, sf, _timeout);
  117.                         /*
  118.                         client.Send("da inviare", Encoding.UTF8, _timeout);
  119.  
  120.                         // risposta (ritorna null se va in timeout)
  121.                         string reply = client.Receive(Encoding.UTF8, _timeout);
  122.                         Console.WriteLine("Received !!");*/
  123.  
  124.                         byte[] reply = new byte[4];
  125.                         client.Receive(reply, _timeout);
  126.  
  127.                         Console.WriteLine("Received !!");
  128.  
  129.                         client.Close();
  130.                     }
  131.                 }
  132.             #endif
  133.             }            
  134.             #if(CLRZMQ_225)
  135.             catch (ZMQ.Exception ex)
  136.             {
  137.                 Console.WriteLine(ex.Message);
  138.             }
  139.             #elif (CLRZMQ_3 || CLRZMQ_2)
  140.                         catch (Exception ex)
  141.                         {
  142.                             Console.WriteLine(ex.Message);
  143.                         }
  144.             #endif
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement