2013-04-14 112 views
0

我有網絡客戶端/服務器應用程序,使用java zeromq 框架的通信。 我有主服務器和N個客戶端輪詢服務器。當服務器上線時。 客戶連接他和他們之間短暫的按摩 直到現在我完成了單客戶端,它工作正常。 但添加另一個客戶端(即2) 我進入請求空作爲返回按摩: request = socket.recv(0); 基礎上,這個例子: http://zguide.zeromq.org/java:mtserverzeromq套接字recv返回null(java warper)

我的代碼(它的一部分了很長) 所有背景和ZeroMq設置被設置,而不是空 和我百達得到這個異常:

Exception in thread "Thread-1" org.zeromq.ZMQException: Operation cannot be accomplished in current state(0x9523dfb) 
     at org.zeromq.ZMQ$Socket.recv(Native Method) 
     at com.controller.core.Daemon$1.run(Daemon.java:127) 



for(int thread_nbr = 0; thread_nbr < m_iThreadPoolCount; thread_nbr++) { 
       Thread worker_routine = new Thread() { 

        @Override 
        public void run() { 
        //synchronized(OBJ_LOCK) { 
         ZMQ.Socket socket = m_pNetworkManager.getContext().socket(ZMQ.REP);//context.socket(ZMQ.REP);      
         socket.connect ("inproc://workers"); 

         while (true) { 

         /** Wait for next request from client (C string) */ 
         byte[] request = null; 
         try{ 
          if(m_pNetworkManager.getContext()!=null) // its never null 
          { 
           request = socket.recv (0); 

          } 

         }catch (Exception e) 
         { 

        // it allays gets null exception    
         } 

         boolean bFoundInList = false; 

         if(request!=null) 
         { 

             // multi frame sending 
             socket.send(m_UT.getbyteArray(
                m_UT.getReplayStructure(aStateMap_replay) 
             ),ZMQ.SNDMORE);          
             socket.send(new byte[0], ZMQ.SNDMORE); 
             byte[] byteFileStruct = null;          
             byteFileStruct = m_UT.serialize(stateFilesStruct); 
             boolean send = socket.send(byteFileStruct,0); 



         } // socket.recv end 

        } 
        // }// synchronized block 
       } 
       }; //Thread worker_routine 

       worker_routine.start(); 
      } 
      // Connect work threads to client threads via a queue 
      ZMQQueue zMQQueue = new ZMQQueue(m_pNetworkManager.getContext(), 
               m_pNetworkManager.getClients(), 
               m_pNetworkManager.getWorkers()); 
      zMQQueue.run(); 

      // We never get here but clean up anyhow 
      m_pNetworkManager.getClients().close(); 
      m_pNetworkManager.getWorkers().close(); 
      m_pNetworkManager.getContext().term(); 
    } 

allso添加了NetworkManager類

public class NetworkManager { 

    /** ZeroMQ context */ 
    private ZMQ.Context m_context = null; 
    /** ZeroMQ socket */ 
    private ZMQ.Socket m_socket = null; 
    /** representation of the clients */ 
    ZMQ.Socket m_clients = null; 
    /** representation of the workers threads */ 
    ZMQ.Socket m_workers = null; 
    /** 
    * NetworkManager constructor. 
    */ 
    public NetworkManager() 
    { 
     ; 
    } 

    /** 
    * Setup the network ZeroMQ network layer 
    * @param sControllerDomain the Controller domain name and port 
    */ 
    public void Init(String sControllerDomain) 
    { 
     /** Prepare our context and socket */ 
     m_context = ZMQ.context(1); 

     m_clients = m_context.socket(ZMQ.ROUTER); 
     // m_clients = m_context.socket(ZMQ.REP); 
     m_clients.bind (sControllerDomain); 

     m_workers = m_context.socket(ZMQ.DEALER); 
     m_workers.bind ("inproc://workers"); 
    } 

    /** 
    * Get ZeroMQ context 
    * @return ZMQ.Context 
    */ 
    public ZMQ.Context getContext() { 
     return m_context; 
    } 

    /** 
    * get ZeroMQ Socket 
    * @return ZMQ.Socket 
    */ 
    public ZMQ.Socket getSocket() { 
     return m_socket; 
    } 
    /** 
    * get the workers as ZMQ.Socket 
    * @return ZMQ.Socket 
    */ 
    public ZMQ.Socket getWorkers() { 
     return m_workers; 
    } 
    /** 
    * get the Clients as ZMQ.Socket 
    * @return ZMQ.Socket 
    */ 
    public ZMQ.Socket getClients() { 
     return m_clients; 
    } 

} 
+0

根據[This SO answer](http://stackoverflow.com/a/14651272/1122039),您在多個線程中使用相同的ZeroMQ實例,這是一個不允許的情況。 – Cebence

+0

但是m_pNetworkManager.getContext()。socket(ZMQ.REP); 在每個線程中創建了新的套接字 – user63898

+0

我相信他們也提到你很少需要多個實例。 – Cebence

回答

0

什麼是您的OS系統?如果您使用Windows,則不支持使用以下操作:m_workers.bind ("inproc://workers")。 IIRC。