views:

93

answers:

2

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!
+1  A: 

I'm not familiar with netcat, but is it possible that it's just not printing the "HALLO" response because it's encoded in a way that netcat can't make sense of (i.e. Java's serialization format)? Does it work if you don't wrap the output stream in an ObjectOutputStream?

Aaron Novstrup
+1  A: 

If this code is truly the code you are using, it has a serious problem: it is using threads from inside a static initializer.

Here:

object HelloWorld extends {

Extends what?

def main(args: Array[String]) {}

No main method, so everything else is inside the constructor to object HelloWorld. Basically, that means everything using threads (including actors) is unreliable. Put this stuff inside the main method.

Daniel
sometimes you do not see the forest for the trees. That was the problem. Many thanks, I was just dumb.
evildead