How can I write a unsigned 16 bit int to the send ...
# ktor
z
How can I write a unsigned 16 bit int to the send channel in a unix socket connection? There's
writeShort
but that's for signed int
e
you could just use
writeShort(ushort.toShort())
,
UShort
and
Short
have the same representation
there is a writeUShort which does exactly that, on ktor types
z
im still having some issue cause im not getting any response in the read channel
I found some more information on the data it expects https://www.x.org/releases/X11R7.7/doc/xproto/x11protocol.html#Encoding::Connection_Setup but not really sure how to send this with ktor
Copy code
val readChannel = clientConnection.openReadChannel()
val sendChannel = clientConnection.openWriteChannel(autoFlush = true)
val authName = ""
val authData = ""

fun BytePacketBuilder.writePadding(bytes: Int) {
    repeat((4 - bytes % 4) % 4) { writeByte(0) }
}

sendChannel.writePacket {
    writeUByte(102u)
    writeUByte(0u)
    writeUShort(11u)
    writeUShort(0u)
    writeUShort(authName.length.toUShort())
    writeUShort(authData.length.toUShort())
    writeUShort(0u)
    writeText(authName)
    writePadding(authName.length)
    writeText(authData)
    writePadding(authData.length)
}

println(readChannel.readByte())
It just throws
Uncaught Kotlin exception: <http://io.ktor.utils.io|io.ktor.utils.io>.errors.EOFException: 1 bytes required but EOF reached
at the readByte call