golang socket断点续传大文件的实现方法

2020-01-28 13:58:08丽君

客户端


// file name: client.go

package main

import (
 "os"
 "io"
 "net"
 "log"
 "time"
 "strconv"
)

// 获取服务端发送的消息
func clientRead(conn net.Conn) int {
 buf := make([]byte, 5)
 n, err := conn.Read(buf)
 if err != nil {
  log.Fatalf("receive server info faild: %sn", err)
 }
 // string conver int
 off, err := strconv.Atoi(string(buf[:n]))
 if err != nil {
  log.Fatalf("string conver int faild: %sn", err)
 }
 return off
}

// 发送消息到服务端
func clientWrite(conn net.Conn, data []byte) {
 _, err := conn.Write(data)
 if err != nil {
  log.Fatalf("send 【%s】 content faild: %sn", string(data), err)
 }
 log.Printf("send 【%s】 content successn", string(data))
}

// client conn
func clientConn(conn net.Conn) {
 defer conn.Close()

 // 发送"start-->"消息通知服务端,我要开始发送文件内容了
 // 你赶紧告诉我你那边已经接收了多少内容,我从你已经接收的内容处开始继续发送
 clientWrite(conn, []byte("start-->"))
 off := clientRead(conn)

 // send file content
 fp, err := os.OpenFile("test.txt", os.O_RDONLY, 0755)
 if err != nil {
  log.Fatalf("open file faild: %sn", err)
 }
 defer fp.Close()

 // set file seek
 // 设置从哪里开始读取文件内容
 _, err = fp.Seek(int64(off), 0)
 if err != nil {
  log.Fatalf("set file seek faild: %sn", err)
 }
 log.Printf("read file at seek: %dn", off)

 for {
  // 每次发送10个字节大小的内容
  data := make([]byte, 10)
  n, err := fp.Read(data)
  if err != nil {
   if err == io.EOF {
    // 如果已经读取完文件内容
    // 就发送'<--end'消息通知服务端,文件内容发送完了
    time.Sleep(time.Second * 1)
    clientWrite(conn, []byte("<--end"))
    log.Println("send all content, now quit")
    break
   }
   log.Fatalf("read file err: %sn", err)
  }
  // 发送文件内容到服务端
  clientWrite(conn, data[:n])
 }
}

func main() {
 // connect timeout 10s
 conn, err := net.DialTimeout("tcp", ":8888", time.Second * 10)
 if err != nil {
  log.Fatalf("client dial faild: %sn", err)
 }
 clientConn(conn)
 }

客户端读取文件test.txt内容发送到服务端,服务端把接收到的文件内容保存在test_1.txt文件中。我们模拟断点续传的方式是:

第一次先发送test.txt文件内容到服务端

修改test.txt文件,加一些内容

再次运行server socket以及client socket,观察客户端是不是只发送新增的文件内容到服务端


# 假设我的test.txt文件有以下内容
$ cat test.txt
hello golang.

# 先运行server socket再运行client socket(分别在两个终端窗口运行)
$ go run server.go
$ go run client.go

# 服务端会输出以下内容
2018/04/05 23:37:13 waiting accept.
2018/04/05 23:37:15 recevice 8 bytes, content is 【start-->】
2018/04/05 23:37:15 file size: 0
2018/04/05 23:37:15 recevice 10 bytes, content is 【hello gola】
2018/04/05 23:37:15 append content: 【hello gola】 success
2018/04/05 23:37:15 recevice 2 bytes, content is 【n.】
2018/04/05 23:37:15 append content: 【n.】 success
2018/04/05 23:37:16 recevice 6 bytes, content is 【<--end】
2018/04/05 23:37:16 receive over
exit status 1

# 客户端会输出如下内容
2018/04/05 23:37:15 send 【start-->】 content success
2018/04/05 23:37:15 read file at seek: 0
2018/04/05 23:37:15 send 【hello gola】 content success
2018/04/05 23:37:15 send 【n.】 content success
2018/04/05 23:37:16 send 【<--end】 content success
2018/04/05 23:37:16 send all content, now quit

# 这时候我们看看test_1.txt内容跟test.txt完全一致
$ cat test_1.txt
hello golan.

# ------- 模拟断点续传 ----------
# 现在我们往test.txt追加内容: hello python.
$ cat test.txt
hello golang.
hello python.

# 我们再一次运行server socket 和 client socket(分别在两个终端窗口运行)
$ go run server.go
$ go run client.go

# 服务端会输出以下内容
2018/04/05 23:44:31 waiting accept.
2018/04/05 23:44:34 recevice 8 bytes, content is 【start-->】
2018/04/05 23:44:34 file size: 12
2018/04/05 23:44:34 recevice 10 bytes, content is 【
hello pyt】
2018/04/05 23:44:34 append content: 【
hello pyt】 success
2018/04/05 23:44:34 recevice 4 bytes, content is 【hon.】
2018/04/05 23:44:34 append content: 【hon.】 success
2018/04/05 23:44:35 recevice 6 bytes, content is 【<--end】
2018/04/05 23:44:35 receive over
exit status 1
# 服务端在接收到客户端发送的 start--> 信息后会获取上次接收到文件内容位置,并通知客户端(这里file size 是12)

# 客户端会输出以下内容
2018/04/05 23:44:34 send 【start-->】 content success
2018/04/05 23:44:34 read file at seek: 12
2018/04/05 23:44:34 send 【
hello pyt】 content success
2018/04/05 23:44:34 send 【hon.】 content success
2018/04/05 23:44:35 send 【<--end】 content success
2018/04/05 23:44:35 send all content, now quit
# 我们客户端获取到了服务端返回的文件位置,通过 Seek 来指定从哪里开始读取文件
# 通过日志可以看到我们客户端只发送了后面追加的内容: hello python. 到服务端

# 我们看看此时test_1.txt文件的内容是否跟test.txt一致
$ cat test_1.txt
hello golang.
hello python.