1
回答
大家帮忙看看这个用Socket实现的EchoServer有什么问题
终于搞明白,存储TCO原来是这样算的>>>   
依次启动EchoServer和EchoClient, EchoServer收到EchoClient的请求,但是EchoServer在读取EchoClient发送过来的信息时,阻塞了。
package socket.echo

import actors.Actor
import java.net.{InetSocketAddress, Socket, ServerSocket}
import java.io.{ByteArrayOutputStream, InputStream, OutputStream}

/**
 * Created by aiguozhe
 * User: aiguozhe
 * Date: 11-11-3
 * Time: 下午4:02
 * To change this template use File | Settings | File Templates.
 */

object EchoServer {
  def main(args: Array[String]): Unit = {
    val port = if (args.length > 1 && args(0) != null) args(0).toInt else 8080;
    val backlog = if (args.length > 2 && args(1) != null) args(1).toInt else 50;
    new EchoServer(port, backlog).start()
  }
}

class EchoServer(val port: Int, val backlog: Int) extends Actor {
  def act() {
    val serverSocket = new ServerSocket(port, backlog)
    println("Create a serverSocket: " + serverSocket)
    var isStop = false
    while (!isStop) {
      val socket = serverSocket.accept();
      println("Create a socket " + socket)
      Actor.actor {
        val is = socket.getInputStream
        val os = socket.getOutputStream
        val buf = IO.read(is)
        os.write(buf)
        os.flush()
        is.close()
        os.close()
        val msg = new String(buf, "UTF-8")
        println("Receive " + msg + " from " + socket)
        if ("quit".equalsIgnoreCase(msg)) isStop = true
        socket.close()
      }
    }
    println("Server stop! ")
  }
}

object IO {
  val BYTE_BLOCK_LENGTH = 1024
  /**
   * The input and output stream won't be closed after the method invocation
   *
   * @param is
   * @param os
   */
  def copy(is: InputStream, os: OutputStream): Unit = {
    var buf = new Array[Byte](BYTE_BLOCK_LENGTH)
    var isStop = false
    while (!isStop) {
      val len = is.read(buf, 0, buf.length);
      if (len == -1) isStop = true
      else os.write(buf, 0, len)
    }
  }

  def read(is: InputStream, encoding: String): String = {
    var resBytes: Array[Byte] = read(is)
    return new String(resBytes, encoding)
  }

  def read(is: InputStream): Array[Byte] = {
    var baos: ByteArrayOutputStream = new ByteArrayOutputStream(BYTE_BLOCK_LENGTH)
    copy(is, baos)
    baos.flush
    var resBytes: Array[Byte] = baos.toByteArray
    is.close
    baos.close
    return resBytes
  }
}

object EchoClient {
  def main(args: Array[String]): Unit = {
    val ip = if (args.length > 1 && args(0) != null) args(0) else "127.0.0.1";
    val port = if (args.length > 2 && args(1) != null) args(1).toInt else 8080
    val n = if (args.length > 3 && args(2) != null) args(2).toInt else 100
    val isReuseAddr = if (args.length > 4 && args(3) != null) args(3).toBoolean else false

    for (i <- 0 until n) {
      val socket = createSocket(ip, port, isReuseAddr)
      val msg = "hello"
      println("send " + msg + " to " + socket.getRemoteSocketAddress)
      val is = socket.getInputStream()
      val os = socket.getOutputStream()
      os.write(msg.getBytes("UTF-8"))
      os.flush()
      IO.read(is)
      is.close
      os.close
      socket.close
    }


  }

  private def createSocket(ip: String, port: Int, isReuseAddr: Boolean): Socket = {
    val socket = new Socket()
    socket.setReuseAddress(isReuseAddr)
    socket.connect(new InetSocketAddress(ip, port))
    return socket;
  }
}

举报
mj4738
发帖于6年前 1回/426阅

以下是问题补充:

  • @mj4738 :这是采用Scala编写的 (6年前)
顶部