An easy-to-use abstraction for networking in Kotlin.
KNet was created to simplify communication between multiple servers running a Kotlin application. Instead of individually setting up connections and sending packets, it works by creating interfaces and generating an automatic implementation that instead sends a network request to another server and runs the implementation defined there.
In order to allow proxying, all methods in an interface must be suspending (kotlin coroutines), and the interface must be annotated with ProxiedInterface
@ProxiedInterface interface TestInterface { suspend fun test(arg: String?): String suspend fun testList(arg: List<String>): String }
To establish communication, one application must act as the server, and another as the client. A server can have multiple clients, but a single client cannot connect to multiple servers (though you could create multiple clients in a single application to connect to different servers).
These are created as follows
val server = KNetServer(3500) val client = KNetClient(InetSocketAddress("localhost", 3500))
Now registering an implementation of an interface is as simple as calling
server.registerProxy<TestInterface>(TestInterfaceImpl())
A proxy to the implementation on the server can be obtained from the client using
val proxy = client.getProxy<TestInterface>()
Where proxy
is now an implementation of TestInterface
that automatically sends a request to the server when a method is called, and receives a response (or possibly an exception).
Exceptions thrown in the implementation are also sent to the caller allowing exception handling on the client's side.
A client can also register a proxy which can be called by the server, however to get a proxy to a client, the specific ServerConnection
is required, all connections are in server.connected
.
I initially decided to write this project when I got tired of the old system we had, which required manually creating request and response data classes with serialization. Of course, I could have used an existing library, but I decided it would be a nice learning experience, and it might help me understand why some libraries make certain choices in their design. I had previously written a compiler plugin for Kotlin (see KComp), but I wanted to try using the much more stable KSP API which provides annotation processing for Kotlin.
Created: 7/14/2025
Last updated: 7/17/2025