1、请求/应答模式(REP/REQ)

该模式特征:

  • 服务器使用REP类型套接字而客户端使用REQ类型套接字
  • 客户端发送请求和接收答复,而服务器则接收请求并发送答复
  • 客户端可以连接到一个或多个服务器。在这种情况下,请求会在所有的服务器(Reps)之间循环,一个请求被发送到某个服务器,下一个请求则被发送到下个服务器,如此进行下去。
  • 基于状态的模式:客户端在发送另一个请求之前,必须先接收前一个请求的答复。而服务器在接收另一个请求之前,必须答复前一个请求。

【ZeroMQ】消息模式

 1 //服务器端代码
 2         private static void Main(string[] args)
 3         {
 4             using (var context = ZContext.Create())
 5             {
 6                 using (var resp = new ZSocket(context, ZSocketType.REP))
 7                 {
 8                     resp.Bind("tcp://*:5555");
 9                     while (true)
10                     {
11                         ZFrame reply = resp.ReceiveFrame();
12                         string message = reply.ReadString();
13 
14                         Console.WriteLine("Receive message {0}", message);
15 
16                         resp.Send(new ZFrame(message));
17 
18                         if (message == "exit")
19                         {
20                             break;
21                         }
22                     }
23                 }
24             }
25         }
服务器端代码

相关文章: