简体   繁体   English

Scala套接字无法写入输出流

[英]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. 如果我收到一个HELO,我想对一条消息作出反应,但我无法以任何方式写入套接字。 I used nc to listen for incoming connections: 我用nc来监听传入的连接:

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. 在netcat中,但客户端没有发送任何答案。

Here is my scala code: 这是我的scala代码:

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. 接收工作正常,并且actor对传入消息作出反应,但写入永远不会完成。 Am I just oversee something, or is my code wrong for sending to an outputSteam. 我只是监督一些事情,或者我的代码是错误的发送到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 . 没有main方法,所以其他所有内容都在构造函数内部以对象HelloWorld Basically, that means everything using threads (including actors) is unreliable. 基本上,这意味着使用线程(包括演员)的一切都是不可靠的。 Put this stuff inside the main method. 把这些东西放在main方法中。

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 (ie Java's serialization format)? 我不熟悉netcat,但它是否可能不打印"HALLO"响应,因为它的编码方式是netcat无法理解的(即Java的序列化格式)? Does it work if you don't wrap the output stream in an ObjectOutputStream ? 如果不将输出流包装在ObjectOutputStream是否有效?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM