我有两个简单的组件,它们应该使用REQ / REP ZeroMQ模式相互通信。 服务器(REP Socket)使用pyzmq:
在Python中实现import zmq
def launch_server():
print "Launching server"
with zmq.Context.instance() as ctx:
socket = ctx.socket(zmq.REP)
socket.bind('tcp://127.0.0.1:5555')
while True:
msg = socket.recv()
print "EOM\n\n"
使用NetMQ库用C#编写的客户端(REQ套接字):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetMQ;
namespace PyNetMQTest
{
class Program
{
static void Main(string[] args)
{
string msg;
NetMQ.Sockets.RequestSocket socket = new NetMQ.Sockets.RequestSocket();
socket.Connect("tcp://127.0.0.1:5555");
for(int i=0; i<5; i++)
socket.SendFrame("test_"+i);
}
}
}
通过与使用Python实现的REQ套接字交谈,Python Server实现已经过测试并且工作正常。但是C#REQ套接字在循环的第一次迭代中抛出以下错误,并且没有任何消息到达服务器:
未处理的类型&#39; NetMQ.FiniteStateMachineException&#39;发生在NetMQ.dll中 附加信息:Req.XSend - 无法发送其他请求
堆栈跟踪:
at NetMQ.Core.Patterns.Req.XSend(Msg& msg)
at NetMQ.Core.SocketBase.TrySend(Msg& msg, TimeSpan timeout, Boolean more)
at NetMQ.NetMQSocket.TrySend(Msg& msg, TimeSpan timeout, Boolean more)
at NetMQ.OutgoingSocketExtensions.Send(IOutgoingSocket socket, Msg& msg, Boolean more)
at NetMQ.OutgoingSocketExtensions.SendFrame(IOutgoingSocket socket, String message, Boolean more)
at PyNetMQTest.Program.Main(String[] args) in d:\users\emes\documents\visual studio 2015\Projects\PyNetMQ Test\PyNetMQTest\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
这是我使用ZMQ的第一步,C#代码来自库documentation。 是什么让代码抛出此错误?
我正在使用:
======================解决方案===================== =
正如@somdoron在他的回答中解释的那样,roor casue认为两个套接字都需要在发送/接收的整个周期之前才能重复使用。 事实上,在python中实现的REP套接字并没有改变它的状态,因此错误在于python和C#代码。这是固定代码:
REP Socket
import zmq
def launch_server():
print "Launching server"
with zmq.Context.instance() as ctx:
socket = ctx.socket(zmq.REP)
socket.bind('tcp://127.0.0.1:5555')
while True:
msg = socket.recv()
socket.send("reply to "+msg)
print "EOM\n\n"
REQ Socket
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetMQ;
namespace PyNetMQTest
{
class Program
{
static void Main(string[] args)
{
NetMQ.Sockets.RequestSocket socket = new NetMQ.Sockets.RequestSocket();
socket.Connect("tcp://127.0.0.1:5555");
string msg, reply;
while (true)
{
Console.WriteLine("Type message: ");
msg = Console.ReadLine();
Console.WriteLine("Sending : " + msg);
socket.SendFrame(msg);
reply = socket.ReceiveFrameString();
Console.WriteLine("Received: " + reply + Environment.NewLine);
}
}
}
}
答案 0 :(得分:5)
请求和响应套接字是状态机,请求必须先发送然后再调用Receive,不能连续调用5次发送。
如果响应相反,则必须先调用Receive。
如果一方只发送而另一方只接收您可以使用推拉模式而不是Req-Rep。如果需要两种方式通信,您也可以使用经销商 - 路由器。无论如何,似乎Req-Rep的使用是不正确的。