我写了一个send
和receive
'\0'
终止字符串
https://gist.github.com/jilen/10a664cd588af10b7d09
object Foo {
implicit val S = scalaz.concurrent.Strategy.DefaultStrategy
implicit val AG = tcp.DefaultAsynchronousChannelGroup
...
def runServer() {
def writeStr(str: String) = tcp.write(ByteVector(str.getBytes))
val echoServer = (readStr |> serverLog("[Server] Receiving")).flatMap(writeStr)
val server = tcp.server(addr, concurrentRequests = 1)(echoServer.repeat)
server.flatMap(_.drain).run.run
}
def runClient() {
val topic = async.topic[String]()
val echoClient = (topic.subscribe |> clientLog("[Client] Inputing")).map { str =>
tcp.write(ByteVector(str.getBytes) ++ Delimiter) ++ (readStr |> clientLog("[Client] Receiving"))
}
val client = tcp.connect(addr)(tcp.lift(echoClient))
client.run.runAsync(println)
io.stdInLines.to(topic.publish).run.run
}
}
我在不同的终端上运行Foo.runServer()
和Foo.runClient()
从客户端控制台输入数字 1 2 3 ... ,但客户端没有收到回复。
我的echo应用程序有什么问题?
答案 0 :(得分:0)
最后我将客户端重写为
val echoClient = tcp.subscribe.map(str => ByteVector(str.getBytes) ++ Delimiter)
val client = tcp.connect(addr)(tcp.writes(echoClient))
它有效