用scala实现一个基于TCP Socket的快速文件传输程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用scala实现一个基于TCP Socket的快速文件传输程序相关的知识,希望对你有一定的参考价值。

这是用scala实现的一个简单的文件传输程序。

服务端

package jpush

import java.io.{DataInputStream, File, FileOutputStream}
import java.net.ServerSocket

import scala.collection.JavaConversions._

/**
  * Created by dingb on 2016/6/3.
  */
object Server extends  App {
  def port = 8899
  override def main(args: Array[String]) {
    val roots = args.toList match {
      case Nil => List(new File(System.getProperty("user.dir")))
      case _ =>  args.map(new File(_)).toList
    }

    printf("dst root is %s\n", roots.mkString(","))
    val ss = new ServerSocket(port)
    def accept: Unit = {
      val s = ss.accept()
      printf("%s in \n", s.getRemoteSocketAddress);
      async {
        val dis = new DataInputStream(s.getInputStream)
        val fn = dis.readUTF
        val size = dis.readLong
        printf("loading %s %d\n", fn, size);
        if(size > 0) {
          roots.foreach(new File(_, fn).getParentFile.mkdirs())
          val oses = roots.map(new File(_, fn)).map(new FileOutputStream(_))
          val buf = new Array[Byte](1024)
          var done = false
          while(!done) {
            val r = dis.read(buf)
            if(r <= 0) done = true
            else oses.foreach(_.write(buf, 0, r))
          }
          oses.foreach(_.close)
        }
      }
      accept
    }
    accept
  }

  def async(body : => Unit) = {
    val t = new Thread(new Runnable {
      override def run(): Unit = {
        body
      }
    })
    t.start()
  }
}

 

客户端

package jpush

import java.io.{DataOutputStream, File, FileInputStream}
import java.net.Socket

/**
  * Created by dingb on 2016/6/3.
  */
object Client  extends  App{

  def send_file(ip: String, file: File, top: String): Unit = {
    val fis = new FileInputStream(file)
    val s = new Socket(ip, Server.port)
    val dos = new DataOutputStream(s.getOutputStream)
    val remotename = top + File.separator + file.getName
    printf("sending %d bytes %s to %s\n", file.length(), file.getAbsolutePath, remotename)
    dos.writeUTF(remotename)
    dos.writeLong(file.length())
    val buf = new Array[Byte](1024)
    var done = false
    while(!done) {
      val r = fis.read(buf)
      if(r <= 0) done = true
      else dos.write(buf, 0, r)
    }
    dos.close()
    fis.close()
    s.close()

  }

  def send(ip: String, file: File, top: String): Unit = {
    if(file.isDirectory) file.listFiles().foreach(send(ip, _, "" + File.separator + file.getName))
    else send_file(ip, file, top)

  }

  override def main(args: Array[String]) {
    if(args.length < 1)  usage()
    else args.drop(1).map(new File(_)).foreach(send(args(0), _, ""))

  }

  def usage(): Unit = {
    println("usage: jpsh.Client <ip> [file]...")
    System.exit(-1)
  }
}

 

源代码下载

https://github.com/dingbig/jpush

以上是关于用scala实现一个基于TCP Socket的快速文件传输程序的主要内容,如果未能解决你的问题,请参考以下文章

基于TCP协议的项目架构之Socket流传输的实现

C++语言实现网络聊天程序的设计与实现(基于TCP/IP协议的SOCKET编程)超详细(代码+解析)

使用PHP Socket开发Yar TCP服务

Socket网络编程学习笔记 TCP快速入门

Java Socket实现基于TCP和UDP多线程通信

JAVA Socket 底层是怎样基于TCP/IP 实现的???