https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

Kwabena Berko

10/24/2023, 12:20 PM
Quick
ktor tcp
question. For the below code on a
jvm
environment, when the server abruptly closes, we immediately get a null for
receiveChannel.readUTF8Line()
, but on
iOS
we never get a null and
readUTF8Line()
continues to suspend. Is there a workaround to this? Thanks in advance.
Copy code
val selectorManager = SelectorManager(<http://Dispatchers.IO|Dispatchers.IO>)
        val socket = aSocket(selectorManager).tcp().connect("127.0.0.1", 9002)

        val receiveChannel = socket.openReadChannel()
        val sendChannel = socket.openWriteChannel(autoFlush = true)

        launch(<http://Dispatchers.IO|Dispatchers.IO>) {
            while (true) {
                val greeting = receiveChannel.readUTF8Line()
                if (greeting != null) {
                    println(greeting)
                } else {
                    println("Server closed a connection")
                    socket.close()
                    selectorManager.close()
                    exitProcess(0)
                }
            }
        }