alexandrgomd
alexandrgomd

Reputation: 11

Error while trying to pass Byte through TCP connection

I need to pass byte like "0x02" through tcp connection and process it on a server. Code of client is here:

package protocol
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Tcp}
import akka.util.ByteString
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Tcp}
import akka.util.ByteString
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

class UpperServiceClient(ip: String, port: Int) {
    def run = {
        implicit val system = ActorSystem("ClientSys")
        implicit val materializer = ActorMaterializer()

        val a1 = Array("0x02", "0x02").toSeq
        val testInput = a1.map(ByteString(_))
        val result: Future[ByteString] = Source(testInput).via(Tcp().outgoingConnection(ip, port)).
          runFold(ByteString.empty) { (acc, in) => acc ++ in }

        val res: ByteString = Await.result(result, 10.seconds)
    }
}

But IDEA shows me error:

Type mismatch, expected: Iterable[NotInferedT], actual: Seq[ByteString]

What should I do to pass "0x02" as whole byte?

Upvotes: 0

Views: 90

Answers (1)

Jeffrey Chung
Jeffrey Chung

Reputation: 19527

The Source factory method that you're using expects an immutable Iterable. By default, calling .toSeq on an array returns a mutable Seq. One way to address this is to call .toList instead:

val a1 = Array("0x02", "0x02").toList

Upvotes: 1

Related Questions