package Ġcom . li am co al studio . ket tle . networking . main Ċ Ċ import Ġcom . li am co al studio . ket tle . logging . Console Logger Ċ import Ġcom . li am co al studio . ket tle . servers . java . Java Server Ċ import Ġjava . net . Inet SocketAddress Ċ import Ġjava . nio . channels . As ynchronous Server SocketChannel Ċ import Ġjava . nio . channels . As ynchronous SocketChannel Ċ Ċ class ĠTCP Server ( port : ĠInt ) Ġ{ ĊĠĠĠ Ġprivate Ġval Ġlogger Ġ= ĠConsole Logger ( TCP Server :: class ) ĊĠĠĠ Ġprivate Ġval Ġsocket Channel : ĠAs ynchronous Server SocketChannel Ġ= ĊĠĠĠĠĠĠĠ ĠAs ynchronous Server SocketChannel . open (). bind ( Inet SocketAddress ( port )) ĊĊĠĠĠ Ġclass ĠCompletion Handler ( val Ġhandler : Ġ( TCP Server , ĠClient ) Ġ-> ĠUnit , Ġprivate Ġval Ġserver : ĠTCP Server ) Ġ: Ġjava . nio . channels . Completion Handler < As ynchronous SocketChannel , ĠTCP Server > Ġ{ ĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġcompleted ( p 0 : ĠAs ynchronous SocketChannel ?, Ġp 1 : ĠTCP Server ?) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġserver . logger . info (" Client Ġconnected ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġhandler ( p 1 !! , ĠClient ( p 0 !! )) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġfailed ( p 0 : ĠThrowable ?, Ġp 1 : ĠTCP Server ?) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġp 0 ?. printStackTrace () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġdestroy () Ġ{ ĊĠĠĠĠĠĠĠ Ġsocket Channel . close () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġaccept ( handler : Ġ( TCP Server , ĠClient ) Ġ-> ĠUnit ) Ġ{ ĊĠĠĠĠĠĠĠ Ġsocket Channel . accept ( this , ĠCompletion Handler ( handler , Ġthis )) ĊĠĠĠ Ġ} Ċ }