zoukankan      html  css  js  c++  java
  • 学习进度笔记

    学习进度笔记23

    模拟器代码

    import java.io.{PrintWriter}

    import java.net.ServerSocket

    import scala.io.Source

    object StreamingSimulation {

      // 定义随机获取整数的方法

      def index(length: Int) = {

        import java.util.Random

        val rdm = new Random

        rdm.nextInt(length)

      }

      def main(args: Array[String]) {

        // 调用该模拟器需要三个参数,分为为文件路径、端口号和间隔时间(单位:毫秒)

        if (args.length != 3) {

          System.err.println("Usage: <filename> <port> <millisecond>")

          System.exit(1)

        }

        // 获取指定文件总的行数

        val filename = args(0)

        val lines = Source.fromFile(filename).getLines.toList

        val filerow = lines.length

        // 指定监听某端口,当外部程序请求时建立连接

        val listener = new ServerSocket(args(1).toInt)

        while (true) {

          val socket = listener.accept()

          new Thread() {

            override def run = {

              println("Got client connected from: " + socket.getInetAddress)

              val out = new PrintWriter(socket.getOutputStream(), true)

              while (true) {

                Thread.sleep(args(2).toLong)

                // 当该端口接受请求时,随机获取某行数据发送给对方

                val content = lines(index(filerow))

                println(content)

                out.write(content + ' ')

                out.flush()

              }

              socket.close()

            }

          }.start()

        }

      }

    }

  • 相关阅读:
    读取points文件
    JSP语法1
    servlet与SSI
    JDBC连接数据库
    django开发Blog(2)
    django开发Blog(1)
    JSP学习2:useBean动作标签
    django开发Blog(4)
    Servelet基础
    servlet会话管理2
  • 原文地址:https://www.cnblogs.com/xueqiuxiang/p/14467002.html
Copyright © 2011-2022 走看看