我有两个简单的组件,它们应该使用REQ/REP ZeroMQ模式相互通信。服务器(REP )是在Python中使用pyzmq实现的:
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"使用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实现已经过测试,工作正常。但是,C# reach在循环的第一次迭代中抛出以下错误,任何消息都不会到达服务器:
类型'NetMQ.FiniteStateMachineException‘的未处理异常发生在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#代码是从文档库中提取的。是什么使代码抛出这个错误的?
我正在使用:
======================解决方案======================
正如@somdoron在他的回答中所解释的那样,这两个插座都需要经过整个发送/接收周期,才能被重用。事实上,在python中实现的REP套接字也没有改变它的状态,因此错误出现在python和C#代码中。这是固定代码:
REP插座
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插座
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);
}
}
}
}发布于 2016-10-05 08:13:44
请求和响应套接字是状态机,您必须先发送请求,然后调用接收,您不能调用5个连续发送。
与之相反的是,你必须先呼叫接收。
如果一方只发送,另一方只接收,你可以使用推拉模式,而不是Req。你也可以使用交易商-路由器,如果需要,双方沟通。无论如何,Rep的用法似乎是不正确的。
https://stackoverflow.com/questions/39860614
复制相似问题