Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simplest possible line-by-line server socket server in Scala?

Tags:

sockets

scala

I'm writing a small MUD style game in Scala. I want to allow users to telnet in to the game (just like in the old days). This really is just a game. It's going to run on a private network. Security is not (yet) a consideration.

The "protocol" consists of lines of text (terminated with CR) sent by the user. The server will reply with lines of text of it's own before waiting for the next line of user's input. In effect this is a REPL style text interface.

Is there a handy library that will do the networking stuff for me? I just want it to open up a port and allow users to connect to the service and start sending and receiving text from the game.

There are plenty of full-featured server libraries (e.g. TwitterServer) which seem to be geared towards Thrift and HTTP. I'm actually after something much simpler. I just want to receive lines of text, and respond with lines of text.

Any ideas about how I might be able to achieve this with the greatest possible simplicity?

like image 523
Salim Fadhley Avatar asked Feb 07 '23 18:02

Salim Fadhley


1 Answers

Here you go:

  val acceptor = new ServerSocket(port)
  while(true) {
    val socket = acceptor.accept
    Future { serve(socket.getInputStream, socket.getOutputStream) } onComplete { socket.close() }
  } 
like image 61
Dima Avatar answered May 10 '23 21:05

Dima