gpt4 book ai didi

scala - 经销商(服务器)到经销商( worker )不工作

转载 作者:行者123 更新时间:2023-12-04 15:54:33 27 4
gpt4 key购买 nike

在以下DEALERDEALER连接, worker DEALER发送 [][Foo!] ,即一个 2 帧的消息,到服务器 DEALER .

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)
}
}
}

但是,在运行它时,只有打印语句显示了 Worker 消息的发送。未显示服务器的实际收据
[info] Running net.async.DealerToDealer
[info] Worker Bob: sending Empty + Foo!.
[info] Worker Bob: sent

为什么 Server收到这条消息?

另外,是 [][Message] DEALER 格式正确至 DEALER沟通?

最佳答案

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

关于scala - 经销商(服务器)到经销商( worker )不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38948235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com