Cherrio LLC
10/16/2025, 12:15 PMAleksei Tirman [JB]
10/17/2025, 5:13 AMThe only really reliable way to detect a lost TCP connection is to write to it. Eventually this will throw anHere is an example of how to detect the disconnection:, but it takes at least two writes due to buffering.IOException: connection reset
val selectorManager = SelectorManager(<http://Dispatchers.IO|Dispatchers.IO>)
val socket = aSocket(selectorManager).tcp().connect("127.0.0.1", 12345)
socket.openWriteChannel(autoFlush = true).use {
while (true) {
try {
writeStringUtf8("Hello, world!\n")
} catch (_: ClosedWriteChannelException) {
println("Disconnected")
break
}
delay(500)
}
}