Right now all the server does is listen for connections and print out what clients send to it, nothing special. There are some downsides to the way I'm doing things. The actor class by default spawns it's own thread which can get ugly fast however it is easy enough to convert over to coroutine like behavior. The reason I haven't done this yet is because I'm not using select to see if there is data so waiting for one receive or in this case line would block the whole thread and that wouldn't be nice. Anyways here is my code.
- package stdServer
- object Server
- {
- {
- {
- println("Listening on port " + port)
- {
- numClients += 1
- }
- listener.close()
- }
- {
- System.err.println("Could not listen on port: " + port + ".")
- System.exit(-1)
- }
- }
- }
- {
- def act
- {
- {
- print("Client connected from " + socket.getInetAddress() + ":" + socket.getPort)
- println(" assigning id " + clientId)
- {
- println(clientId + ") " + inputLine)
- inputLine = in.readLine()
- }
- socket.close()
- println("Client " + clientId + " quit")
- }
- {
- System.err.println(e)
- System.err.println(e.printStackTrace())
- case e =>
- System.err.println("Unknown error " + e)
- }
- }
- }
One of things I like about this code is that it shows off mixing Java code with Scala. If you haven't noticed it fits right in and you can't really tell that there is Java embedded.
I also wrote a version of this code with Scala's remote actors however it seems limiting. When I tried to connect to telnet and typed something my server instantly crashed with an out of memory error. I'm guessing that remote actors only can only talk to each other which means that I would have to learn the protocol that it is using and mimic it in my client thats not written in Scala and then hope no one tried to use telnet on it. I'd rather just use normal TCP/IP sockets and know that it will work with pretty much anything.
Well thats it for now, next time I hope to have an non-blocking version of this code with concurrent actors and perhaps some basic functionality.
Hi,
ReplyDeletegreat post first of all. Actors though don't seem to be used in the Actor way. Their basic benefit is async message sharing and a big number of them available. Seams more that you have used them as a normal thread.