title: Why would nanomsg hang?
body: <p>I just started doing nnanomsg programming with C#.  For a simplistic client / server example, I'm starting with the following example where a client connects with a server to get the next number.  For now, the example is for localhost, except I have my network IP address hardcoded just to make sure that works.  There would be potentially many clients connecting to the server, however this example accesses the server far more aggressively than it would be in real life.</p>
  <p>Client Code:</p>
  <pre><code>using NNanomsg.Protocols;
  using System;
  using System.Text;
  using System.Threading;
  
  namespace Client {
      class Program {
          static void Main(string[] args) {
              string connect = &quot;tcp://127.0.1.107:9595&quot;;
              while (true) {
                  PairSocket s = new PairSocket();
                  s.Connect(connect);
                  s.Send(Encoding.UTF8.GetBytes(&quot;Hello!&quot;));
                  Console.WriteLine(&quot;Sent&quot;);
                  byte[] ss = null;
                  while (ss == null) {
                      ss = s.ReceiveImmediate(); 
                      Thread.Sleep(250);
                  }
                  s.Dispose();
                  Console.WriteLine(&quot;Received: &quot; + Encoding.UTF8.GetString(ss));
  }   }   }   }
  </code></pre>
  <p>Server code:</p>
  <pre><code>using System;
  using System.Text;
  using NNanomsg.Protocols;
  
  namespace Server {
      public class Server {
          public static int ITER = 0;
          public static int NEXT_ID = 10001;
          public static string CONNECT;
          public static int PORT = 9595;
          public static string QUERY;
          public static string TCP = &quot;tcp://127.0.1.107&quot;;
  
          static void Main(string[] args) {
              string bind = TCP + &quot;:&quot; + PORT;
              Console.WriteLine(&quot;server starting...&quot;);
              PairSocket s = new PairSocket();
              s.Bind(bind);
              while (true) {
                  byte[] ss = null;
                  ss = s.Receive();
                  Console.WriteLine(&quot;Received from client: &quot; + Encoding.UTF8.GetString(ss));
                  byte[] b = Encoding.UTF8.GetBytes(NEXT_ID.ToString());
                  Console.WriteLine(&quot;Sent next id &quot; + NEXT_ID);
                  s.Send(b);
                  NEXT_ID++;
                  if (NEXT_ID &gt; 65535) { NEXT_ID = 10001; }
  }   }   }   }
  </code></pre>
  <p>In general, this works swimmingly when there is a server and a single client.  However, when I start a second client on the same physical machine, they work together for a while, for maybe 5 to 10 iterations, then one of the clients, usually the one which was most recently started, hangs.  Adding delays (<code>Thread.sleep(5000)</code> for example) improves the situation slightly, however it will eventually hang after maybe 50 iterations.  Currently, the client stops immediately after displaying &quot;Sent&quot;.  I haven't yet tried starting a client from another physical machine on the network.</p>
  <p>Also, whether I wait in a loop with <code>ReceiveImmediate()</code> or just sit at a <code>Receive()</code> does not seem to matter.</p>
  <p>What I expect is that every client would have a chance to send the server a request and have a reply.  The exact order is not that important, as long as the server eventually responds to a client's request.</p>
  <p>I would like to understand why it hangs.  I have the latest version of the DLL, 1.1.5.</p>
  <p>Thanks very much for every bit of assistance.</p>