我正在尝试使用ZeroMQ在VS中实现 ROUTER / ROUTER
模式,而我似乎无法让这件事发挥作用。据我所知,我知道服务器工作正常,因为我尝试使用其他类型的客户端套接字没有任何问题。但是当它成为ROUTER
时,它不起作用。
有人会知道如何修复客户端吗?
我的代码如下:
static string ip = "127.0.0.1";
static string port = "5555";
static string endpoint = string.Format("tcp://{0}:{1}", ip, port);
static void Main(string[] args)
{
Task.Factory.StartNew(StartRouterServer,TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() => StartRouterClient(0), TaskCreationOptions.LongRunning);
Thread.Sleep(10000000);
}
static void StartRouterClient(int i)
{
using (ZContext context = new ZContext())
using (ZSocket router = new ZSocket(context, ZSocketType.ROUTER))
{
router.IdentityString = "client: " + i;
router.Connect(endpoint);
//send message
router.SendMore(new ZFrame(router.IdentityString));
router.SendMore(new ZFrame());
router.Send(new ZFrame("Hi"));
using (ZMessage received = router.ReceiveMessage())
{//on receiving a reply from the server, you come here.
Console.WriteLine("Received a message back from the server!!");
}
}
}
static void StartRouterServer()
{
using (ZContext context = new ZContext())
using (ZSocket router = new ZSocket(context, ZSocketType.ROUTER))
{
router.IdentityString = "router";
router.Bind(endpoint);
Console.WriteLine("The server is bound to {0}", router.LastEndpoint);
while (true)
{
using (ZMessage received = router.ReceiveMessage())
{//what you do when you receive the message.
Console.WriteLine("received a message!");
router.SendMore(received[0]);
router.SendMore(new ZFrame());
router.Send(new ZFrame("Hi back!!"));
}
}
}
}
答案 0 :(得分:1)
在服务器套接字绑定到端口5555之前,可能会创建客户端套接字并尝试发送。如果发生这种情况,客户端套接字将丢弃该消息。
尝试在In [32]: pd.date_range(df.beginning_date.min(), df.end_date.max())
Out[32]:
DatetimeIndex(['2009-12-14', '2009-12-15', '2009-12-16', '2009-12-17', '2009-12-18', '2009-12-19', '2009-12-20', '2009-12-21', '2009-12-22',
'2009-12-23',
...
'2013-12-23', '2013-12-24', '2013-12-25', '2013-12-26', '2013-12-27', '2013-12-28', '2013-12-29', '2013-12-30', '2013-12-31',
'2014-01-01'],
dtype='datetime64[ns]', length=1480, freq='D')
的调用之间添加 Thread.Sleep( 1000 )
,以允许创建服务器套接字时间并绑定。
(我在python中编写了一个类似的代码,只要在客户端连接并发送之前服务器套接字肯定被绑定,它就可以工作。)
答案 1 :(得分:0)
好的,恩,我搞砸了这个问题。对于这种模式,每个路由器都需要知道彼此的身份。这就是它的工作方式。
即在StartRouterClient()方法中,
我不得不替换router.SendMore(new ZFrame(router.IdentityString));
与router.SendMore(server.IdentityString);