I'm using the current Ktor release 3.0.1 to implement a low-level socket client for a server software that has a bit of a weird behavior:
First, you connect to the server using a pseudo-HTTP request secured via TLS. After that, the connection is kept open and you may send unencrypted data via this connection.
I'm trying to implement this use case via Ktor Sockets, but it seems that my use case wasn't anticipated in the current library state, or at least not in an obvious way.
I tried opening the socket via
val rawSocket = aSocket(selectorManager).tcp().connect(host, port)
val secureSocket = rawSocket.tls(Dispatchers.Default)
However, after that, I cannot call rawSocket.openReadChannel()
to read data from the unencrypted connection. Calling this method results in an IllegalStateException
, because Socket.tls()
already opens read and write channels on the raw connection to manage its own state.
Effectively, it seems that a raw socket isn't usable anymore as soon as the TLS layer is started on it. Is there a way to implement my use case with Ktor Sockets, or do I have to fall back to standard Java sockets, where managing sockets is more difficult but I can achieve what I want?