Skip to content

Instantly share code, notes, and snippets.

@aneury1
Forked from limpid-kzonix/akka-tcp.scala
Created November 10, 2022 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aneury1/8ef8dbb944038fd24ece4f3ced2dc70e to your computer and use it in GitHub Desktop.
Save aneury1/8ef8dbb944038fd24ece4f3ced2dc70e to your computer and use it in GitHub Desktop.
simple Scala TCP server
import akka.actor._
import java.net.InetSocketAddress
import akka.util.ByteString
class TCPServer(port: Int) extends Actor {
override def preStart {
IOManager(context.system).listen(new InetSocketAddress(port))
}
def receive = {
case IO.NewClient(server) =>
server.accept()
case IO.Read(rHandle, bytes) => {
val byteString = ByteString("HTTP/1.1 200 OK\r\n\r\nOK")
rHandle.asSocket.write(byteString)
rHandle.close()
}
}
}
object Application {
def main(args: Array[String]) {
val port = 8000
ActorSystem().actorOf(Props(new TCPServer(port)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment