经销商(服务器)到经销商(工人)不工作

时间:2016-08-15 01:40:48

标签: scala zeromq

在以下ATDT15551234 DEALER连接中,工作人员DEALER向服务器DEALER发送[][Foo!],即2帧消息。< / p>

DEALER

但是,在运行它时,只有print语句显示发送Worker的消息。未显示服务器的实际收据

package net.async

import org.zeromq.ZMQ
import org.zeromq.ZMQ.Socket

class Worker(name: String) extends Runnable {

  import DealerToDealer._

  val WorkerResponse = "Foo!".getBytes

  val id = name.getBytes

  override def run(): Unit = {
    val context = ZMQ.context(1)
    val worker = context.socket(ZMQ.DEALER)
    worker.setIdentity(id)
    worker.connect(s"tcp://localhost:$Port")
    runHelper(worker)
  }

  private def runHelper(worker: Socket): Unit = {
    println(s"Worker ${name}: sending Empty + ${show(WorkerResponse)}.")
    worker.send(Empty, ZMQ.SNDMORE)
    worker.send(WorkerResponse, 0)
    println(s"Worker ${name}: sent")
    val message = show(worker.recv(0))
    println(s"Worker ${show(worker.getIdentity)}: received message: $message.")
    runHelper(worker)
  }
}

object DealerToDealer {

  def show(xs: Array[Byte]): String =
    new String(xs)

  val Port = 5555

  val Empty = "".getBytes

  val WorkerMessage = "Bar!".getBytes
  val WorkLimit = 25

  def main(xs: Array[String]): Unit = {
    val context = ZMQ.context(1)
    val server = context.socket(ZMQ.DEALER)
    new Thread(new Worker("Bob")).run()
    server.bind(s"tcp://*:$Port")
    runHelper(server, 0)
  }

  private def runHelper(server: Socket, sent: Int): Unit = {
    if (sent > WorkLimit ) {
      println(s"SERVER: sent the '$WorkerMessage' message ${WorkLimit} times. shutting down.")
      sys.exit(0)
    }
    else {
      println(s"SERVER: sending Empty + ${show(WorkerMessage)}.")
      server.send(Empty, ZMQ.SNDMORE)
      server.send(WorkerMessage, 0)
      println(s"SERVER: sent")
      val message = server.recv(0)
      println(s"SERVER: received message: ${show(message)}.")
      runHelper(server, sent + 1)
    }
  }
}

为什么[info] Running net.async.DealerToDealer [info] Worker Bob: sending Empty + Foo!. [info] Worker Bob: sent 没有收到此消息?

此外,Server格式对[][Message]DEALER通信是否正确?

1 个答案:

答案 0 :(得分:1)

是[] [消息]的格式是正确的,但请记住您需要阅读(所有内容)空框架然后再显示您的消息。