Help with Ktor (Raw sockets)


#1

So, I was gonna make a small IRC client and thought about using Ktor as the networking library.I quickly followed the guide and made this small snippet as an example:

import io.ktor.network.sockets.*
import kotlinx.coroutines.experimental.*
import java.net.*


fun main(args: Array<String>) {
    runBlocking {
        val socket = aSocket().tcp().connect(InetSocketAddress("127.0.0.1", 2323))
        val input = socket.openReadChannel()
        val output = socket.openWriteChannel(autoFlush = true)

        output.writeBytes("hello\r\n")
        val response = input.readASCIILine()
        println("Server said: '$response'")
    }
}

But the problem is that im getting a missing dependencies for the following:

~/Programming_Workspace/Ma_Currents/KtorExample/src/main/kotlin/com/otakusenpai/KtorExample/main.kt: (9, 22): Unresolved reference: aSocket
~//Programming_Workspace/Ma_Currents/KtorExample/src/main/kotlin/com/otakusenpai/KtorExample/main.kt: (2, 16): Unresolved reference: network

Can you guys please help me?


#2

Also, IntelliJ isnt showing hints on the packages i should use. If it did so, then it could be done already.


#3

Unresolved reference means that the variable isn’t defined. You probably meant to type “Socket” instead of “aSocket” or “ASocket”.

I can’t comment on what ktor package you should use, though.

EDIT: You should probably show off the contents of your build.gradle file or talk about how you’re trying to build it for that to be answered. The master branch of ktor makes it look like io.ktor.network.sockets should be a valid package.

EDIT EDIT: Okay, I can reproduce this now that the build.gradle is out. It looks like aSocket is a special function in the Builders. I think it may be a matter of needing to include ktor-network in the build file.


#4

Heres my build.gradle`


#5

I think you need to include compile "io.ktor:ktor-network:$ktor_version" in your dependencies. That should resolve network being undefined.