-
-
Save aneury1/8ef8dbb944038fd24ece4f3ced2dc70e to your computer and use it in GitHub Desktop.
simple Scala TCP server
This file contains hidden or 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 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