Skip to content

edgesider/kssocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KSSocket

可直接用于Kotlin协程的socket实现。

例子

  • TCP客户端
fun main() = runBlocking {
    ASocketChannel.open().let { sock ->
        sock.connect(InetSocketAddress("localhost", 8000))
        sock.write(ByteBuffer.wrap("kssocket".toByteArray()))
        sock.close()
    }
}
  • Socks代理
fun main() = runBlocking {
    val sock = ASocketChannel.open(
        InetSocketAddress("google.com", 80),
        proxy = Socks4Proxy("127.0.0.1", 1080, remoteDns = true)
    )
    sock.writeAll(
        ByteBuffer.wrap(
            ("GET / HTTP/1.1\r\nHost: google.com\r\n" +
                    "User-Agent: curl/7.70.0\r\nAccept: */*\r\n\r\n")
                .toByteArray()
        )
    )
    val buf = ByteBuffer.allocate(1000)
    sock.read(buf)
    buf.flip()
    println(String(ByteArray(buf.limit()).also { buf.get(it) }))
    Unit
}

TODO

  • Socks/Socks5支持
  • DNS异步查询
  • IPv6支持

About

Kotlin协程框架下的socket实现

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages