2016-12-22 5 views
0

私はZeroMQを使ってVSのROUTER/ROUTERパターンを実装しようとしています。私が知ることから、問題のない他のタイプのクライアントソケットで試してみたので、サーバはうまく動作することが分かりました。しかし、それをROUTERにすると動作しません。ZeroMQ ROUTER/ROUTERパターンが機能しないのはなぜですか?

どのようにクライアントを修正する方法についてのアイデアはありますか?次のように

私のコードは次のとおりです。

static string ip = "127.0.0.1"; 
static string port = "5555"; 
static string endpoint = string.Format("tcp://{0}:{1}", ip, port); 

static void Main(string[] args) 
    { 
     Task.Factory.StartNew(StartRouterServer,TaskCreationOptions.LongRunning); 
     Task.Factory.StartNew(() => StartRouterClient(0), TaskCreationOptions.LongRunning); 

     Thread.Sleep(10000000); 
    } 

static void StartRouterClient(int i) 
    { 
     using (ZContext context = new ZContext()) 
     using (ZSocket router = new ZSocket(context, ZSocketType.ROUTER)) 
     { 
      router.IdentityString = "client: " + i; 
      router.Connect(endpoint); 
      //send message 
      router.SendMore(new ZFrame(router.IdentityString)); 
      router.SendMore(new ZFrame()); 
      router.Send(new ZFrame("Hi")); 

      using (ZMessage received = router.ReceiveMessage()) 
      {//on receiving a reply from the server, you come here. 
       Console.WriteLine("Received a message back from the server!!"); 
      } 
     } 
    } 

static void StartRouterServer() 
    { 
     using (ZContext context = new ZContext()) 
     using (ZSocket router = new ZSocket(context, ZSocketType.ROUTER)) 
     { 
      router.IdentityString = "router"; 
      router.Bind(endpoint); 
      Console.WriteLine("The server is bound to {0}", router.LastEndpoint); 

      while (true) 
      { 
       using (ZMessage received = router.ReceiveMessage()) 
       {//what you do when you receive the message. 
        Console.WriteLine("received a message!"); 
        router.SendMore(received[0]); 
        router.SendMore(new ZFrame()); 
        router.Send(new ZFrame("Hi back!!")); 
       } 
      } 
     } 
    } 

答えて

0

さて、NVM、私はアドレッシングを台無しに。このパターンでは、各ルータは互いのアイデンティティを知る必要があります。それがこの仕組みです。 StartRouterClient()メソッドで 即ち、 私は私が試みrouter.SendMore(server.IdentityString);

1

クライアントソケットを作成し、それがクライアントソケットがメッセージをドロップします発生した場合、サーバソケットがポート5555にバインドされる前に送信しようとしていることが可能です。

は、サーバソケットを作成するための時間とバインドを許可するようにTask.Factory.StartNew()への呼び出しの間Thread.Sleep(1000)を入れてみてください。

(限り、サーバソケットは、クライアントが接続する前にバインドして送信することが確信していたとして、私はPythonで同様のコードを書き、それが働いた。)

+0

router.SendMore(new ZFrame(router.IdentityString)); を交換しなければなりませんでした。動作しませんでした。私が犯したのはそれだった。しかし、助けてくれてありがとう! – rmehta

関連する問題