https://kotlinlang.org logo
Title
l

Lost Illusion

09/24/2021, 10:59 PM
I have a UDP raw socket that I want to transmit data through very rapidly (every x ms). I was wondering if there is any difference between using the
SendChannel
provided or whether I should be using
openWriteChannel
and sending my data through the returned
ByteWriteChannel
instead.
a

Aleksei Tirman [JB]

09/27/2021, 9:53 AM
I think it's better to use the
SendChannel
because it's already available. Here is an example:
val address = NetworkAddress("127.0.0.1", 12345)
val socketBuilder: UDPSocketBuilder = aSocket(ActorSelectorManager(<http://Dispatchers.IO|Dispatchers.IO>)).udp()
val socket = socketBuilder.connect(address)

val packet = BytePacketBuilder().apply {
    writeFully("test".toByteArray())
}.build()

socket.outgoing.send(Datagram(address = address, packet = packet))