返回顶部

收藏

Scala 之 Socket 通讯示例

更多

scala代码

package examples

import java.io._
import java.net.{InetAddress,ServerSocket,Socket,SocketException}
import java.util.Random

/** 
 * Simple client/server application using Java sockets. 
 * 
 * The server simply generates random integer values and 
 * the clients provide a filter function to the server 
 * to get only values they interested in (eg. even or 
 * odd values, and so on). 
 */
object randomclient {

  def main(args: Array[String]) {
    val filter: Int => Boolean = try {
      Integer.parseInt(args(0)) match {
        case 1 => x: Int => x % 2 != 0
        case 2 => x: Int => x % 2 == 0
        case _ => x: Int => x != 0
      }
    }
    catch {
      case _ => x: Int => x < 100
    }

    try {
      val ia = InetAddress.getByName("localhost")
      val socket = new Socket(ia, 9999)
      val out = new ObjectOutputStream(
        new DataOutputStream(socket.getOutputStream()))
      val in = new DataInputStream(socket.getInputStream())

      out.writeObject(filter)
      out.flush()

      while (true) {
        val x = in.readInt()
        println("x = " + x)
      }
      out.close()
      in.close()
      socket.close()
    }
    catch {
      case e: IOException =>
        e.printStackTrace()
    }
  }

}

object randomserver {

  def main(args: Array[String]): Unit = {
    try {
      val listener = new ServerSocket(9999);
      while (true)
        new ServerThread(listener.accept()).start();
      listener.close()
    }
    catch {
      case e: IOException =>
        System.err.println("Could not listen on port: 9999.");
        System.exit(-1)
    }
  }

}

case class ServerThread(socket: Socket) extends Thread("ServerThread") {

  override def run(): Unit = {
    val rand = new Random(System.currentTimeMillis());
    try {
      val out = new DataOutputStream(socket.getOutputStream());
      val in = new ObjectInputStream(
        new DataInputStream(socket.getInputStream()));

      val filter = in.readObject().asInstanceOf[Int => Boolean];

      while (true) {
        var succeeded = false;
        do {
          val x = rand.nextInt(1000);
          succeeded = filter(x);
          if (succeeded) out.writeInt(x)
        } while (! succeeded);
        Thread.sleep(100)
      }

      out.close();
      in.close();
      socket.close()
    }
    catch {
      case e: SocketException =>
        () // avoid stack trace when stopping a client with Ctrl-C
      case e: IOException =>
        e.printStackTrace();
    }
  }

}

标签:Socket,Scala

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. yankay 发表 2013-05-01 18:55:26 Scala Tour - 精选
  2. 博主 发表 2013-12-24 09:48:00 剥开Scala的糖衣(5) -- lazy
  3. hongjiang 发表 2014-06-01 16:24:35 华东地区scala爱好者聚会(2014上海)
  4. 孙鹤 发表 2013-04-23 08:06:11 网络服务器编程之路(5)——用户和权限
  5. Freewind 发表 2011-09-12 17:57:25 scala啊scala
  6. Freewind 发表 2011-09-13 14:35:30 class中定义的apply有什么用?
  7. Freewind 发表 2013-01-01 09:01:28 07. 编写异步的Http应用
  8. hongjiang 发表 2014-10-05 12:20:43 scala雾中风景(16): println(1,2,3)为什么work?
  9. juvenxu 发表 2014-10-28 12:44:34 预防错误的设计
  10. leadwatches 发表 2014-11-07 21:27:55 socket网络粘包
  11. debugo 发表 2015-01-06 11:46:59 JVM进程性能监控工具
  12. root 发表 2015-03-09 06:18:01 Socke—转

发表评论