This message was deleted.
# ktor
s
This message was deleted.
z
Python:
Copy code
import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("127.0.0.1", 23333))

PROTOCOL_NAME = "BitTorrent protocol"
hash = bytes.fromhex("2DB9970534B35CF90EAD415FEC15994E87D3D11B")

client_socket.send(len(PROTOCOL_NAME).to_bytes())
client_socket.send(PROTOCOL_NAME.encode())
client_socket.send(bytes(8))
client_socket.send(hash)

response = client_socket.recv(1024)
print(response)

# Print:
# b'\x13BitTorrent protocol\x00\x00\x00\x00\x00\x10\x00\x05-\xb9\x97\x054\xb3\\\xf9\x0e\xadA_\xec\x15\x99N\x87\xd3\xd1\x1b-qB5030-ak_guMigTtFd'
Kotlin:
Copy code
import io.ktor.network.selector.*
import io.ktor.network.sockets.*
import io.ktor.utils.io.*
import kotlinx.coroutines.*

fun main() {
    runBlocking {
        val selectorManager = SelectorManager(Dispatchers.IO)
        val socket = aSocket(selectorManager).tcp().connect("127.0.0.1", 23333) {
            keepAlive = true
            socketTimeout = 20000
        }
        val receiveChannel = socket.openReadChannel()
        val sendChannel = socket.openWriteChannel(autoFlush = true)

        launch(Dispatchers.IO) {
            while (true) {
                val message = receiveChannel.readUTF8Line()
                if (message != null) {
                    println(message)
                } else {
                    println("Server closed a connection")
                    socket.close()
                    selectorManager.close()
                }
            }
        }

        val PROTOCOL_NAME = "BitTorrent protocol"
        val infoHash = "2DB9970534B35CF90EAD415FEC15994E87D3D11B".hexToByteArray()

        sendChannel.writeByte(PROTOCOL_NAME.length.toByte())
        sendChannel.writeString(PROTOCOL_NAME)
        sendChannel.writeByteArray(ByteArray(8))
        sendChannel.writeByteArray(infoHash)
    }
}

/* Throw:
Exception in thread "main" java.io.IOException: Connection reset
	at io.ktor.utils.io.CloseToken.getCause(CloseToken.kt:37)
	at io.ktor.utils.io.ByteChannel.cancel(ByteChannel.kt:136)
	at io.ktor.utils.io.ByteWriteChannelOperationsKt$writer$job$1.invokeSuspend(ByteWriteChannelOperations.kt:150)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:820)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:717)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:704)
Caused by: java.io.IOException: Connection reset
	at io.ktor.utils.io.CloseToken.<init>(CloseToken.kt:27)
	at io.ktor.utils.io.ByteChannel.cancel(ByteChannel.kt:134)
	... 9 more
Caused by: java.net.SocketException: Connection reset
	at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(Unknown Source)
	at java.base/sun.nio.ch.SocketChannelImpl.read(Unknown Source)
	at io.ktor.network.sockets.CIOReaderKt.readFrom$lambda$0(CIOReader.kt:134)
	at io.ktor.utils.io.ByteWriteChannelOperations_jvmKt.write(ByteWriteChannelOperations.jvm.kt:28)
	at io.ktor.utils.io.ByteWriteChannelOperations_jvmKt.write$default(ByteWriteChannelOperations.jvm.kt:24)
	at io.ktor.network.sockets.CIOReaderKt.readFrom(CIOReader.kt:133)
	at io.ktor.network.sockets.CIOReaderKt.access$readFrom(CIOReader.kt:1)
	at io.ktor.network.sockets.CIOReaderKt$attachForReadingDirectImpl$1.invokeSuspend(CIOReader.kt:109)
	... 8 more
*/