title: How to get nanomsg to automatically reconnect reliably?
body: <p>I am having problems handling reconnects when the server briefly goes offline or totally offline and then comes back up.  I can't get my clients to automatically reconnect. Also, there is no property anywhere where I can see the status of the socket (socket disconnected?) so that I can reconnect manually.  What am I missing?</p>
  
  <p><a href="http://nanomsg.org/v0.5/nn_setsockopt.3.html" rel="nofollow">According to the nanomsg documentation</a>, there is a reconnect interval setting called <code>NN_RECONNECT_IVL</code>.  I can't seem to get it to work.  Consider the following:</p>
  
  <p>I've got a working nanomsg server:</p>
  
  <pre><code>nanocat --bind-local 1234 --bus --interval 1 --ascii --data "hello world"
  </code></pre>
  
  <p>Then I attach to it:</p>
  
  <pre><code>nanocat --connect-local 1234 -A --bus
  </code></pre>
  
  <p>and I see:</p>
  
  <pre><code>hello world
  hello world
  hello world
  </code></pre>
  
  <p>Then, I kill the server and restart it and nanocat doesn't reconnect automatically.  Maybe there's a setting that I am missing?</p>
  
  <p>Next, I built a client in C# using NNanomsg:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          NNanomsg.NanomsgSocket s = new NNanomsg.NanomsgSocket(NNanomsg.Domain.SP, NNanomsg.Protocol.BUS);
  
          // doesn't seem to do anything
          s.Options.ReconnectInterval = new TimeSpan(0, 0, 5);
  
          var e = s.Connect("tcp://127.0.0.1:1234");
  
          while (true)
          {
              byte[] ddd;
              ddd = s.ReceiveImmediate();
              if (ddd != null)
              {
                  string m = UTF8Encoding.UTF8.GetString(ddd);
                  Console.WriteLine(m);
              }
              else
              {
                  // don't peg the CPU
                  Thread.Sleep(100);
              }
          }
      }
  }
  </code></pre>
  
  <p>and I see:</p>
  
  <pre><code>hello world
  hello world
  hello world
  </code></pre>
  
  <p>Then, I kill the server and restart it and my C# client doesn't reconnect automatically.  Maybe there's a setting that I am missing?</p>
  
  <p>Next, I built a client in c:</p>
  
  <pre><code>#include &lt;nanomsg/nn.h&gt;
  #include &lt;nanomsg/bus.h&gt;
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      int sock;
      int recv;
      int reconnect_interval;
      char *buf;
  
      buf = NULL;
      reconnect_interval = 5;
  
      sock = nn_socket (AF_SP, NN_BUS);
  
      nn_setsockopt(sock, NN_SOL_SOCKET , NN_RECONNECT_IVL, &amp;reconnect_interval, sizeof(reconnect_interval));
  
      nn_connect(sock, "tcp://127.0.0.1:1234");
  
      while(1 == 1)
      {
          recv = nn_recv(sock, &amp;buf, NN_MSG, 0);
          if(recv &gt; 0)
          {
              printf("%s\r\n", buf);
              nn_freemsg (buf);
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>and I see:</p>
  
  <pre><code>hello world²²²²½½½½½½½½■ε■ε■
  hello world²²²²½½½½½½½½■ε■ε■
  hello world²²²²½½½½½½½½■ε■ε■
  </code></pre>
  
  <p>(The junk because I guess nanomsg doesn't initialize the buffer and I'm lazily using printf)</p>
  
  <p>Then, I kill the server and restart it and my C client doesn't reconnect automatically.</p>
  
  <p>What am I missing?</p>
  
  <p>NOTE:
  I experimented setting the socket option before and also after the <code>nn_connect()</code> and <code>s.Connect</code>.  Nope.</p>