-2

I cant get my head around this one here I am a beginner to Scala just few weeks old and have tried but failed I have read and tried about Actors, Futures,...etc didnt work for me

Could you supply a code of a server client example (or at least the server side) Suppose to open connection using a socket that receives a string (i.e. file path) from several clients and process each one in a thread

import java.net.{Socket, ServerSocket}
import java.util.concurrent.{Executors, ExecutorService}
import java.util.Date
import java.io._
import scala.io._
import java.nio._
import java.util._
import scala.util.control.Breaks
import java.security.MessageDigest
import java.security.DigestInputStream
import scala.util.Sorting

class NetworkService(port: Int, poolSize: Int) extends Runnable {
  val serverSocket = new ServerSocket(port)
  val pool: ExecutorService = Executors.newFixedThreadPool(poolSize)

  def run() {
    try {
      var i = 0
      while (true) {
        // This will block until a connection comes in.
        val socket = serverSocket.accept()
        val in = new BufferedReader(new InputStreamReader(socket.getInputStream)).readLine
        /*var f = new FileSplit(in) //FileSplit is another class that i would like each                   
                                    // client's sent string to be passed as an instance of 
          f.move*/
        pool.execute(new Handler(socket))

      }
    } finally {
      pool.shutdown()


    }
  }

}

class Handler(socket: Socket) extends Runnable {
  def message = (Thread.currentThread.getName() + "\n").getBytes

  def run() {
    socket.getOutputStream.write(message)
    socket.getOutputStream.close()

  }
}


object MyServer {
  def main(args: Array[String]) {
(new NetworkService(2030, 2)).run

}
}
M.Mohy
  • 37
  • 1
  • 7
  • plz zend me da codez? What have you tried and where did you failed? We can help with that. – maasg Jun 09 '14 at 20:26
  • trial code has been added you can take a look – M.Mohy Jun 10 '14 at 05:22
  • "it doesn't work for me" is inadequate. Where are you stuck? What parts are working/not working? What are the symptoms? What have you tried? – The Archetypal Paul Jun 10 '14 at 08:15
  • I am specifically stuck at receiving thread string from each a client and handling it on its own thread (or actor) – M.Mohy Jun 10 '14 at 10:02
  • [val in = new BufferedReader(new InputStreamReader(socket.getInputStream)).readLine] cant be handled outside the while loop because its value change depending on the client – M.Mohy Jun 10 '14 at 10:06
  • I need an actor or some type of listener to listen to each client and say what pass it as an argument for any other method or new instances – M.Mohy Jun 10 '14 at 10:09
  • http://stackoverflow.com/questions/6414942/scala-equivalent-of-python-echo-server-client-example – M.Mohy Jun 13 '14 at 21:31

1 Answers1

2

You have several options available. You could do same old java style app, basically just using java standard libraries and scala syntax.

Maybe this helps: Scala equivalent of python echo server/client example?

You would just need to write logic that handles each socket (the one you get from accept()) in a new thread.

However I would not recommend using plain old java approach directly. There are great libraries out there that can handle that for you. For example Akka:

http://doc.akka.io/docs/akka/2.3.3/scala/io-tcp.html

I would also urge you to read about futures as they are super useful to do stuff async.

Community
  • 1
  • 1
bssstudio
  • 142
  • 7
  • Here: an example of TCP server in Scala with Akka IO: https://gist.github.com/bssstudio/3e5ef7f3d4c14dc4437a – bssstudio Jun 09 '14 at 20:09