3

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.

Client Code:

using NNanomsg.Protocols;
using System;
using System.Text;
using System.Threading;

namespace Client {
    class Program {
        static void Main(string[] args) {
            string connect = "tcp://127.0.1.107:9595";
            while (true) {
                PairSocket s = new PairSocket();
                s.Connect(connect);
                s.Send(Encoding.UTF8.GetBytes("Hello!"));
                Console.WriteLine("Sent");
                byte[] ss = null;
                while (ss == null) {
                    ss = s.ReceiveImmediate(); 
                    Thread.Sleep(250);
                }
                s.Dispose();
                Console.WriteLine("Received: " + Encoding.UTF8.GetString(ss));
}   }   }   }

Server 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 = "tcp://127.0.1.107";

        static void Main(string[] args) {
            string bind = TCP + ":" + PORT;
            Console.WriteLine("server starting...");
            PairSocket s = new PairSocket();
            s.Bind(bind);
            while (true) {
                byte[] ss = null;
                ss = s.Receive();
                Console.WriteLine("Received from client: " + Encoding.UTF8.GetString(ss));
                byte[] b = Encoding.UTF8.GetBytes(NEXT_ID.ToString());
                Console.WriteLine("Sent next id " + NEXT_ID);
                s.Send(b);
                NEXT_ID++;
                if (NEXT_ID > 65535) { NEXT_ID = 10001; }
}   }   }   }

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 (Thread.sleep(5000) for example) improves the situation slightly, however it will eventually hang after maybe 50 iterations. Currently, the client stops immediately after displaying "Sent". I haven't yet tried starting a client from another physical machine on the network.

Also, whether I wait in a loop with ReceiveImmediate() or just sit at a Receive() does not seem to matter.

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.

I would like to understand why it hangs. I have the latest version of the DLL, 1.1.5.

Thanks very much for every bit of assistance.

Bill Early
  • 131
  • 1
  • 8

1 Answers1

0

Simple solution: I switched over to ZeroMQ. Evidently ZeroMQ handles multiple threads far better.

Bill Early
  • 131
  • 1
  • 8