import java.net._
import java.io._
import java.util._
object Server2{
def displayUsageInfo = {
println("Parameters are:");
println(" 1) The port number that the server will listen on")
}
def main(args: Array[String]):Unit = {
if(args.length != 1) {
displayUsageInfo
}
else {
try {
val sock=new ServerSocket(Integer.parseInt(args(0)))
var finished = false
while (!finished){
println("waiting");
val connection=sock.accept()
println("connection from " + connection.getRemoteSocketAddress())
val is = connection.getInputStream()
val in = new BufferedReader(new InputStreamReader(is))
var line = in.readLine
while (line!=null && !finished) {
line match {
case "quit" => finished = true
case _ => println("Received " + line)
}
if(line == "<gia-sou/>"){
printf("he"); //here!!!!!!!!!!
}
if(!finished){
line = in.readLine
}
}
connection.close
}
} catch {
case e:BindException =>
println("Cannot bind to port. Is a server already running?")
case e:NumberFormatException =>
println("Port number should be an integer")
case e:IllegalArgumentException =>
println("The port number needs to be less than 65536")
case ex:Throwable => println("Exception: " +ex.toString())
}
}
}
}
我理解我对Scala的了解非常低,我对此仍然很新。我想知道当我的服务器收到客户端的消息时,我该如何发回消息?喜欢把信息放在哪里的地方就是说'这里'。 请帮忙谢谢。
答案 0 :(得分:0)
您可以使用java提供的任何内容,即PrintWriter
val connection=sock.accept()
println("connection from " + connection.getRemoteSocketAddress())
val printWriter = new PrintWriter(connection.getOutputStream, true)
printWriter.println("Whatever message you like")