UDP Echo Server in Scala
The UDP Echo Server seems to be the 'hello world' of network programming.
What is Scala? Scala is a hybrid OO/functional programming language which runs on the Java Virtual Machine.
I gave it a try after spending three hours trying (and failing) to get the Haskell Eclipse plugin to work on Ubuntu 9.10. This was after spending half a day trying to get Leksah to install. (Which it did, but it still refused to give me auto complete - my most important requirement.) By this point I was really cross with Cabal, hacked-off with Hackage and looking for an alternative functional language which would "just work" in Eclipse.
I had already ruled out Erlang (which I really like) as just too slow for CPU-bound apps, and Clojure because of all the brackets (yes, I know they let me write the AST directly and give me flashy macros, but I want to be able to read my code). Scala is as fast or slow as Java depending on your viewpoint - that's roughly 50% the speed of C++ these days, similar to Haskell's speed.
Installing Scala
- I installed scala with
sudo aptitude install scala- which just worked first time. - I installed the Eclipse plugin from http://www.scala-lang.org/node/94 with four clicks, and one url cut-and-paste.
- Twenty minutes later, I had a working UDP Echo Server.
I really wish other languages were this easy with which to start coding. If you're at all interested in functional programming, I'd recommend Scala as a good place to start. A Java background will make life easier, as you will already be familiar with the libraries - though that's not at all a requirement.
package com.finalcog.udpEchoServer
import java.net.DatagramPacket import java.net.DatagramSocket
object udp_echo_server { val bufsize = 16; val port = 4444;
def main(args : Array[String]) : Unit = {
println("udp echoserver started...")
val sock = new DatagramSocket(port)
val buf = new Array[Byte](bufsize)
val packet = new DatagramPacket(buf, bufsize)
while (true) {
sock.receive(packet)
println("received packet from: " + packet.getAddress())
sock.send(packet)
println("echoed data (first 16 bytess): " +
packet.getData().take(16).toString())
}
} }
Note: The java.net imports are importing Java classes - it's that easy.
P.S. Any feedback on this programme gladly received, it is my first in Scala.
Posted on 06 February 2010.