Hi,
I'm new in the scala world, so excuse my trivial question. :) I just want to open a socket to a port and sand and receive messages. If I receive a HELO, I want to react with a message, but I'm not able to write to the socket in any way. I used nc to listen for incoming connections:
nc -k -l 127.0.0.1 3333
When the client is connected I write
HELO
in the netcat, but the client sends no answer.
Here is my scala code:
package hello
import java.io._
import java.net.{ InetAddress, ServerSocket, Socket, SocketException }
import java.util.Random
import scala.actors.Actor
import scala.actors.Actor._
object HelloWorld extends {
def main(args: Array[String]) {}
val ia = InetAddress.getByName("localhost");
val socket = new Socket(ia, 3333)
val out = new ObjectOutputStream(
new DataOutputStream(this.socket.getOutputStream))
val in = new DataInputStream(socket.getInputStream())
println("Starting client");
var i = 0;
/* The actor!
*
*/
val myActor = actor {
loop {
receive {
case s: String => {
if (s.startsWith("HELO")) {
println("DEBUG: RECEIVED HELO=>SENDING AUTH!")
this.out.writeUTF("HALLO")
this.out.flush();
} else {
println("received:" + s);
}
}
case _ => println("I have no idea what I just got.")
}
}
}
/*
* Testing the actor!
*
*/
myActor ! "foobar";
while (!socket.isConnected()) {
println("Not connected waiting")
Thread.sleep(5000);
}
if (socket.isConnected()) {
println("connected");
}
try {
while (true) {
i += 1;
val x = in.readLine()
myActor ! x;
Thread.sleep(500);
}
} catch {
case e: IOException =>
e.printStackTrace()
}
}
The receiving works just fine, and the actor reacts on the incoming message, but the write is never done. Am I just oversee something, or is my code wrong for sending to an outputSteam.
Heres my output from the console window:
Starting client
connected
received:foobar
DEBUG: RECEIVED HELO=>SENDING AUTH!