我有10个客户端从服务器接收查询,处理查询并将消息大致同时发送到服务器接收端是这样的:
我首先创建套接字:
val commandSocket = zmqContext.createSocket(SocketType.ROUTER)
commandSocket.identity = identity.toByteArray()
commandSocket.bind(commandBindAddr)
然后将查询作为命令发送给所有客户端;
for (client in clientsArray) {
commandSocket.sendMore(client)
commandSocket.sendMore("")
commandSocket.send(queryBytes)
println("command server sent to " + client.decodeToString() )
}
然后我等待回复;……
fun start(): Thread {
return thread(start=true) {
println("CommandServer")
while (true) {
val addrb = commandSocket.recv()
val empty = commandSocket.recv()
val msg = commandSocket.recv()
println("message received from ${String(addrb)}")
bq.add(Pair(addrb, msg))
}
}
}
客户端是这样的:创建套接字:
val querySocket = zmqContext.createSocket(SocketType.REQ)
querySocket.identity = identity.toByteArray()
querySocket.connect(queryAddr)
fun start(): Thread {
val serializer = Cbor()
return thread(start=true) {
println("QueryProcessor")
while (true) {
val recvBytes = querySocket.recv()
if (recvBytes != null) {
val q = serializer.load(Query.serializer(), recvBytes)
val qr = bp.process(q)
val res = serializer.dump(QueryResponse.serializer(), qr)
println("$sending query response back")
querySocket.send(res)
println("${logTS()} sent to parent")
} else {
querySocket.send(null as ByteArray?)
}
}
}
}
客户端接收和处理查询并发送回回复,但是随着客户端数量的增加,我不会在服务器上收到消息。似乎他们不知何故迷路了。
知道如何解决这个问题吗?