Skip to content

Instantly share code, notes, and snippets.

@julien-truffaut
Last active August 29, 2015 14:11
Show Gist options
  • Save julien-truffaut/e0196958e188ca040e4c to your computer and use it in GitHub Desktop.
Save julien-truffaut/e0196958e188ca040e4c to your computer and use it in GitHub Desktop.
How to run a Tcp server in the background
import java.net.InetSocketAddress
import scalaz.concurrent.Task
import scalaz.stream.tcp
import java.util.concurrent.Executors
object TcpServerApp extends App {
val executor = Executors.newSingleThreadExecutor
implicit val S = scalaz.concurrent.Strategy.DefaultStrategy
implicit val AG = tcp.DefaultAsynchronousChannelGroup
val address = new InetSocketAddress("localhost", 9999)
val pipeline = tcp.reads(1024).map(bv => println(s"Received a ByteVector of size: ${bv.size}"))
val server = tcp.server(address, concurrentRequests = 2)(pipeline).runLast.run.getOrElse(sys.error("Plop")).run
Task.fork(server)(executor).runAsync(println)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment