ZeroMQ FiniteStateMachineException in REQ/REP pattern
<p>I have two simple components which are supposed to communicate with each other using the REQ/REP ZeroMQ pattern.
The Server (REP Socket) is implemented in Python using pyzmq:</p>

<pre><code>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"
</code></pre>

<p>The Client (REQ socket) written in C# using the NetMQ library:</p>

<pre><code>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&lt;5; i++)
                socket.SendFrame("test_"+i);
        }
    }
}
</code></pre>

<p>The Python Server implementation has been tested and works fine by talking to a REQ socket implemented using Python. But the C# REQ socket throws the following error within the first iteration of the loop and no messages reach the Server whatsoever:</p>

<p><strong>An unhandled exception of type 'NetMQ.FiniteStateMachineException' occurred in NetMQ.dll
Additional information: Req.XSend - cannot send another request</strong></p>

<p>Stacktrace:</p>

<pre><code>at NetMQ.Core.Patterns.Req.XSend(Msg&amp; msg)
   at NetMQ.Core.SocketBase.TrySend(Msg&amp; msg, TimeSpan timeout, Boolean more)
   at NetMQ.NetMQSocket.TrySend(Msg&amp; msg, TimeSpan timeout, Boolean more)
   at NetMQ.OutgoingSocketExtensions.Send(IOutgoingSocket socket, Msg&amp; 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()
</code></pre>

<p>These are the first my first steps with ZMQ and the C# code is taken from the library <a href="http://netmq.readthedocs.io/en/latest/introduction/" rel="nofollow">documentation</a>. <strong>What makes the code throw this error?</strong></p>

<p>I am using:</p>

<ul>
<li>pyzmq 14.7</li>
<li>NetMQ 3.3.3.4  </li>
<li>.NET 4.6</li>
</ul>

<hr>

<p><strong>====================== SOLUTION ======================</strong></p>

<p>As explained by @somdoron in his answer, th roor casue was that both sockets need to go the full cycle of send/receive before beeing able to be reused.
As a matter of fact the REP socket implemented in python did not ever change it's state either so the error was in both, the python AND the C# code. Here is the fixed code:</p>

<p>REP Socket</p>

<pre><code>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"
</code></pre>

<p>REQ Socket</p>

<pre><code>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);
            }
        }
    }
}
</code></pre>

<p>Request and Response sockets are state machines, with Request you must first Send and then call Receive, you cannot call 5 consecutive Send.</p>

<p>With Response its the opposite, you must call Receive first.</p>

<p>If one side is only sending and the other only receiving you can use Push-Pull pattern instead of Req-Rep. You can also use Dealer-Router if needed both ways communication. Anyway it seems the usage of Req-Rep is incorrect.</p>

c# python zeromq pyzmq netmq