Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple TCP server
import scala.util._
import akka.util._
object SimpleTcpServer extends App {
val address = ""
val port = 6666
val serverLogic = Flow[ByteString]
.via(Framing.delimiter(ByteString("\n"), maximumFrameLength = 256, allowTruncation = true))
.map(msg => s"Server hereby responds to message: $msg\n")
def mkServer(address: String, port: Int)(implicit system: ActorSystem, materializer: Materializer): Unit = {
import system.dispatcher
val connectionHandler = Sink.foreach[Tcp.IncomingConnection] { conn =>
println(s"Incoming connection from: ${conn.remoteAddress}")
val incomingCnnections = Tcp().bind(address, port)
val binding =
binding onComplete {
case Success(b) =>
println(s"Server started, listening on: ${b.localAddress}")
case Failure(e) =>
println(s"Server could not be bound to $address:$port: ${e.getMessage}")
def mkAkkaServer() = {
implicit val server = ActorSystem("Server")
implicit val materializer = ActorMaterializer()
mkServer(address, port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment