开发者

Scala socket cannot write to output stream

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!


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜