我一直在编写一个简单的客户端/服务器应用程序,它在Scala中发回一个响应,我一直试图使用ExecutionContext
多线程处理我的服务器而没有任何运气。
这是我的服务器代码,我认为我的执行块的位置导致错误:
import java.net._;
import java.io._;
import scala.concurrent.ExecutionContext
object ServerTwo extends App {
var s1: Socket = null;
var dis: DataInputStream = null;
var s1In: InputStream= null;
var o1out: OutputStream = null;
var odos: DataOutputStream = null
while(true){
def execute(body: =>Unit)=
ExecutionContext.global.execute( new Runnable {def run()= body})
try{
// Open your connection to a server, at port 1234
var s: ServerSocket = new ServerSocket(999)
s1 = s.accept(); // Wait and accept a connection
execute{
s1In = s1.getInputStream()
dis = new DataInputStream(s1In)
var st = new String (dis.readUTF())
//println("Word received is "+st )
st="Received message";
var o1out: OutputStream = null; var odos: DataOutputStream = null
o1out = s1.getOutputStream()
odos = new DataOutputStream (o1out)
// Send the string back
odos.writeUTF(st);
}//end execute
}//end try
catch {
case e: Exception =>
} finally {
// Close the connection, but not the server socket
try{
s1.close()
}
catch{
case e: Exception =>
}
}
}
}