0

我使用 ZeroMQ 到客户端/服务器应用程序,现在在我的 MT 服务器中我尝试设置超时,我尝试在服务器上设置:

socket.setReceiveTimeOut(2000);
socket.setSendTimeOut(2000);

没有运气,我如何设置响应超时。

这是我的多线程服务器代码,取自 mt 服务器的 zeromq 示例:

/*
 *  Multithreaded Hello World server in Java
 *
 *  @author Vadim Shalts
 *  @email vshalts@gmail.com
 *
 */

import org.zeromq.ZMQ;
import org.zeromq.ZMQQueue;

class mtserver {
   static void main(String[] args) {

      final ZMQ.Context context = ZMQ.context(1);

      ZMQ.Socket clients = context.socket(ZMQ.ROUTER);
      clients.bind ("tcp://*:5555");

      ZMQ.Socket workers = context.socket(ZMQ.DEALER);
      workers.bind ("inproc://workers");

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

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

               while (true) {

                  //  Wait for next request from client (C string)
                  byte[] request = socket.recv (0);
                  System.out.println ("Received request: ["+new String(request,0,request.length-1)+"]");

                  //  Do some 'work'
                  try {
                     Thread.sleep (1000);
                  } catch(InterruptedException e) {
                     e.printStackTrace();
                  }

                  //  Send reply back to client (C string)
                  byte[] reply = "World ".getBytes();
                  reply[reply.length-1] = 0; //Sets the last byte of the reply to 0
                  socket.send(reply, 0);
               }
            }
         };
         worker_routine.start();
      }
      //  Connect work threads to client threads via a queue
      ZMQQueue zMQQueue = new ZMQQueue(context,clients, workers);
      zMQQueue.run();

      //  We never get here but clean up anyhow
      clients.close();
      workers.close();
      context.term();
   }
} 
4

2 回答 2

0

为什么你需要在服务器上超时?

在客户端设置超时的自然位置。

socket.setReceiveTimeOut(int 毫秒); 在客户端工作。

于 2015-04-28T08:56:51.607 回答
0

为什么需要超时?原始示例不使用它。

于 2012-12-30T00:37:53.487 回答