Scala Introduction

Scala is an object-functional programming language for general software applications. Scala programs run inside JVM so it is easy to build hybrid applications with java and scala components running under the same Java Virtual Machine. This post is a brief introduction to this language.

Installation

    sudo apt-get install scala

Hello world

We will create a file named HelloWorld.scala and then We can copy code below:

    object HelloWorld {
      def main(args: Array[String]) {
        println("Hello, world!")
      }
    }

We can compile with scalac HelloWorld.scala this command will generate a class file named HelloWorld.class after that We can execute it with scala HelloWorld

Simple TCP echo server Using java.net.*

We will write a simple echo tcp server, first at all, let's code a java version:


    import java.lang.*;
    import java.io.*;
    import java.net.*;

    class Server {
        public static void main(String args[]) {
            try {
                int serverPort = 4020;
                ServerSocket serverSocket = new ServerSocket(serverPort);
                serverSocket.setSoTimeout(10000);

                while(true) {
                    System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");

                    Socket server = serverSocket.accept();
                    System.out.println("Just connected to " + server.getRemoteSocketAddress());

                    PrintWriter toClient =
                        new PrintWriter(server.getOutputStream(),true);
                    BufferedReader fromClient =
                        new BufferedReader(
                                new InputStreamReader(server.getInputStream()));
                    String line = fromClient.readLine();
                    System.out.println("Server received: " + line);
                    toClient.println(line);
                    server.close();
                }

            } catch(Exception e) {
                System.out.print(e.getMessage());
            }
        }
    }

We can write same code in scala using java.net.* package from java:


import java.net.{ ServerSocket, SocketException, SocketTimeoutException, Socket }
import java.io.{ PrintWriter, BufferedReader, InputStreamReader, OutputStreamWriter }

object EchoServer {
  var port = 1320

  def run() {
    val serverSocket = new ServerSocket(port)

    def process( socket: Socket) {
      val bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream))
      var line = ""
      var msg = ""
        do {
          line = bufferedReader.readLine()
          msg += line + "\n"
        } while (line != "")

      val out: PrintWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream))
      out.println(msg)
      out.flush()
    }

    def loop() {
      while (true) {
        val socket = serverSocket.accept()
        process(socket)
        socket.close()
      }
    }
    loop()
  }

  def main(args: Array[String]) {

    if(args.length > 0) {
      val arglist = args.toList
      port = arglist(0).toInt
    }
    run()
  }

}

References