2014-01-08 20 views
0

我有一个程序,创建一个actor,然后从默认输入读取。 如果我写的一个特点,使其工作于“法”法的基本演员如下因素:斯卡拉ObjectInputStream使程序失败无声

trait SocketActor extends Actor{ 


protected def sock:Socket 


protected val in:BufferedReader=new BufferedReader(new InputStreamReader(this.sock.getInputStream())) 
protected val out:PrintWriter= new PrintWriter(this.sock.getOutputStream(), true) 

def act(){ 
    println("This get to be executed") 
} 

如果我写了下面不执行的行为方法

trait SocketActor extends Actor{ 


protected def sock:Socket 


protected val in:ObjectInputStream=new ObjectInputStream(this.sock.getInputStream()) 
protected val out:ObjectOutputStream = new ObjectOutputStream (this.sock.getOutputStream()) 
def act(){ 
    println("This doesn't get to be executed") 
} 

演员的创作可以恢复如下:

import java.net._ 
import java.io._ 
import scala.io._ 
import game.io._ 
class PlayerActor(protected val sock:Socket) extends { 

} with SocketActor 
object TabuClient{ 
    def main(args:Array[String]){ 
    try{ 
     println("Always exected on both cases") 
     val port=1337 
     val s = new Socket(InetAddress.getByName("localhost"), port) 

     val a=new PlayerActor(s) 
     a.start() 

     for (line <- io.Source.stdin.getLines){ 
      a.sendMessage(line) 
     } 
     s.close() 
    } 
    catch{ 
     case e:Throwable=>{ 
      e.printStackTrace() 
     } 

    } 
} 

两种方式编译,但演员开始后的第二个基本失败,而不抛出异常

回答

0

执行下列对象输入流解决它,逗它足以并不需要懒惰,如果它只是一个InputStream

trait SocketActor extends Actor{ 


protected def sock:Socket 


protected lazy val in:ObjectInputStream=new ObjectInputStream(this.sock.getInputStream()) 
protected lazy val out:ObjectOutputStream = new ObjectOutputStream (this.sock.getOutputStream()) 
def act(){ 
    println("This doesn't get to be executed") 
}