I was trying out the newer api of ktor using raw s...
# ktor
o
I was trying out the newer api of ktor using raw sockets and am having trouble (I'm always getting timeout errors.)
Copy code
override suspend fun receiveUTF8Data(): String? {
        var data: String? = null
        var cB = StringBuilder(512)
        try {
            if(!input.readUTF8LineTo(cB,512))
                throw Exception("BasicConnection.kt: Didn't receive data from connection!")
            else data = cB.toString()
        } catch (e: Throwable) {
            e.printStackTrace()
            socket.close()
            connected = false
            null
        }
        return data
    }
And the call is lke this
Copy code
val job = GlobalScope.async { data = conn.receiveUTF8Data() }
job.await()
The errors are as follows:
Copy code
Connected!
Entering loop...
Received data: :<http://hitchcock.freenode.net|hitchcock.freenode.net> NOTICE * :*** Looking up your hostname...
Received data: :<http://hitchcock.freenode.net|hitchcock.freenode.net> NOTICE * :*** Checking Ident
Received data: :<http://hitchcock.freenode.net|hitchcock.freenode.net> NOTICE * :*** Couldn't look up your hostname
Received data: :<http://hitchcock.freenode.net|hitchcock.freenode.net> NOTICE * :*** No Ident response
Received data: ERROR :Closing Link: 127.0.0.1 (Connection timed out)
java.lang.Exception: BasicConnection.kt: Didn't receive data from connection!
	at com.github.otakusenpai.testbot.connection.BasicConnection.receiveUTF8Data(BasicConnection.kt:103)
	at com.github.otakusenpai.testbot.MainKt$main$1$job$1.doResume(main.kt:86)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
	at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:149)
	at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:13)
	at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Copy code
java.lang.Exception: BasicBot.kt: Null value received from connection!
	at com.github.otakusenpai.testbot.MainKt$main$1.doResume(main.kt:90)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:149)
	at kotlinx.coroutines.experimental.AbstractContinuation.run(AbstractContinuation.kt:19)
	at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:140)
	at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:70)
	at kotlinx.coroutines.experimental.BuildersKt__BuildersKt.runBlocking(Builders.kt:46)
	at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.experimental.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:36)
	at kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Unknown Source)
	at com.github.otakusenpai.testbot.MainKt.main(main.kt:46)
Received data: null
c
so it looks like there are no bytes received so there is nothing to read and decode
o
@cy this happens even if i use readUTF8Line()
c
readLine function simply waits until \r \n \r\n or end of stream
o
but my net is working, and it shows that its connected
c
it returns false if there are no bytes received and the stream is closed
since you have you message
Didn't receive data from connection!
then you simply have no incoming bytes at all
o
is it possible to have the connection open even if no data is send through the channel ?
aka no incoming bytes?
c
why not?
I'd suggest to read about TCP first s
o
does readUTF8Line or readUTF8LineTo do that?
ok
c
No, they don't
they are waiting until a next line is received
the connection is already established before
o
yeah but hav the connection open? until the socket closes it
c
yes, it is just always open until get closed by a peer (slightly simplified but it is )