-
-
Save ivportilla/bbce4e601499c689a12c8c82022f4be8 to your computer and use it in GitHub Desktop.
Simple TCP server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util._ | |
import akka.actor._ | |
import akka.stream._ | |
import akka.stream.scaladsl._ | |
import akka.util._ | |
object SimpleTcpServer extends App { | |
val address = "127.0.0.1" | |
val port = 6666 | |
val serverLogic = Flow[ByteString] | |
.via(Framing.delimiter(ByteString("\n"), maximumFrameLength = 256, allowTruncation = true)) | |
.map(_.utf8String) | |
.map(msg => s"Server hereby responds to message: $msg\n") | |
.map(ByteString(_)) | |
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}") | |
conn.handleWith(serverLogic) | |
} | |
val incomingCnnections = Tcp().bind(address, port) | |
val binding = incomingCnnections.to(connectionHandler).run() | |
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) | |
} | |
mkAkkaServer() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment