[英]Sending non-ByteString over Tcp Client using Akka IO
I am using akka-io to create a Tcp Client Actor that communicates with a non-jvm tcp (non-akka) server over a socket. 我正在使用akka-io创建一个Tcp Client Actor,它通过套接字与非jvm tcp(非akka)服务器通信。 Because akka-io sends ByteString
and the program expects a regular String
outputted to it, the ByteString
message is never processed. 因为akka-io发送ByteString
并且程序期望输出一个常规String
,所以永远不会处理ByteString
消息。 A very crude java based approach to this actually works : 一个非常粗略的基于Java的方法实际上有效 :
override def receive: Receive = {
case ClientTcpActor.Start =>
val socket = new Socket(socketAddress.getHostName, socketAddress.getPort)
val out = new PrintWriter(socket.getOutputStream, true)
out.println("Hello World!")
import java.io.BufferedReader
import java.io.InputStreamReader
val is = socket.getInputStream
val isr = new InputStreamReader(is)
val br = new BufferedReader(isr)
val message = br.readLine
println(s"Message received from the server : $message")
}
I am able to send a message to the tcp port and receive a response back. 我能够向tcp端口发送消息并收到响应。
However, this akka-io approach doesnt work. 但是,这种akka-io方法不起作用。 Specifically, I never receive data back from the socket because the external application cant process the ByteString
sent by the Write
object 具体来说,我从不从套接字接收数据,因为外部应用程序无法处理Write
对象发送的ByteString
def receive: PartialFunction[Any, Unit] = {
case CommandFailed(_: Connect) =>
log.info("Connection failed.")
context stop self
case c@Connected(_, _) =>
log.info("Connect succeeded.")
val connection = sender()
connection ! Register(self)
val message = ByteString("hello world")
log.info(s"Sending request message ${message}")
connection ! Write(message)
case Received(data) =>
log.info(data.toString())
case _: ConnectionClosed =>
log.info("Connected is closed!")
case _ =>
log.info("Something else is up.")
}
I also created my own akka-IO server application that processes data on the same port and verified that I was actually sending the message to the port, but the akka server actor was needed to process the decoding of the ByteString
我还创建了自己的akka-IO服务器应用程序,该应用程序处理同一端口上的数据并验证我实际上是将消息发送到端口,但是需要akka服务器actor来处理ByteString
的解码
Im hoping this is some easy-fix I'm overlooking. 我希望这是一个容易修复,我忽略。
Over the network there is no such thing as String
or ByteString
, there are only bytes. 在网络上没有String
或ByteString
这样的东西,只有字节。 ByteString
is essentially just a wrapper around byte arrays to ensure they are not mutated (as that would then require synchronisation for it to be safe to share between threads/actors). ByteString
本质上只是一个字节数组的包装器,以确保它们不会发生变异(因为它需要同步才能在线程/参与者之间共享)。 When you create a ByteString
from a String
it will use an encoding ( UTF8
in this case) to encode the string into bytes. 从String
创建ByteString
,它将使用编码(在本例中为UTF8
)将字符串编码为字节。
Can't say what is wrong from the pieces of your code that you included, but there is a complete example client in the docs that may be helpful to compare your code to: http://doc.akka.io/docs/akka/current/scala/io-tcp.html#Connecting 不能说你所包含的代码片段有什么问题,但是文档中有一个完整的示例客户端可能有助于将你的代码比较为: http : //doc.akka.io/docs/akka /current/scala/io-tcp.html#Connecting
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.