怎么在golang中利用socket断点续传大文件
本篇文章给大家分享的是有关怎么在golang中利用socket断点续传大文件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联公司主要业务有网站营销策划、网站制作、成都网站建设、微信公众号开发、微信小程序开发、HTML5、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、网络营销推广资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。
服务端
// file name: server.go
package main
import (
"os"
"io"
"net"
"log"
"strconv"
// "time"
)
// 把接收到的内容append到文件
func writeFile(content []byte) {
if len(content) != 0 {
fp, err := os.OpenFile("test_1.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0755)
defer fp.Close()
if err != nil {
log.Fatalf("open file faild: %s\n", err)
}
_, err = fp.Write(content)
if err != nil {
log.Fatalf("append content to file faild: %s\n", err)
}
log.Printf("append content: 【%s】 success\n", string(content))
}
}
// 获取已接收内容的大小
// (断点续传需要把已接收内容大下通知客户端从哪里开始发送文件内容)
func getFileStat() int64 {
fileinfo, err := os.Stat("test_1.txt")
if err != nil {
// 如果首次没有创建test_1.txt文件,则直接返回0
// 告诉客户端从头开始发送文件内容
if os.IsNotExist(err) {
log.Printf("file size: %d\n", 0)
return int64(0)
}
log.Fatalf("get file stat faild: %s\n", err)
}
log.Printf("file size: %d\n", fileinfo.Size())
return fileinfo.Size()
}
func serverConn(conn net.Conn) {
defer conn.Close()
for {
var buf = make([]byte, 10)
n, err := conn.Read(buf)
if err != nil {
if err == io.EOF {
log.Println("server io EOF\n")
return
}
log.Fatalf("server read faild: %s\n", err)
}
log.Printf("recevice %d bytes, content is 【%s】\n", n, string(buf[:n]))
// 判断客户端发送过来的消息
// 如果是'start-->‘则表示需要告诉客户端从哪里开始读取文件数据发送
switch string(buf[:n]) {
case "start-->":
off := getFileStat()
// int conver string
stringoff := strconv.FormatInt(off, 10)
_, err = conn.Write([]byte(stringoff))
if err != nil {
log.Fatalf("server write faild: %s\n", err)
}
continue
case "<--end":
// 如果接收到客户端通知所有文件内容发送完毕消息则退出
log.Fatalf("receive over\n")
return
// default:
// time.Sleep(time.Second * 1)
}
// 把客户端发送的内容保存到文件
writeFile(buf[:n])
}
}
func main() {
// 建立监听
l, err := net.Listen("tcp", ":8888")
if err != nil {
log.Fatalf("error listen: %s\n", err)
}
defer l.Close()
log.Println("waiting accept.")
// 允许客户端连接,在没有客户端连接时,会一直阻塞
conn, err := l.Accept()
if err != nil {
log.Fatalf("accept faild: %s\n", err)
}
serverConn(conn)
}客户端
// 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: %s\n", err)
}
// string conver int
off, err := strconv.Atoi(string(buf[:n]))
if err != nil {
log.Fatalf("string conver int faild: %s\n", err)
}
return off
}
// 发送消息到服务端
func clientWrite(conn net.Conn, data []byte) {
_, err := conn.Write(data)
if err != nil {
log.Fatalf("send 【%s】 content faild: %s\n", string(data), err)
}
log.Printf("send 【%s】 content success\n", 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: %s\n", err)
}
defer fp.Close()
// set file seek
// 设置从哪里开始读取文件内容
_, err = fp.Seek(int64(off), 0)
if err != nil {
log.Fatalf("set file seek faild: %s\n", err)
}
log.Printf("read file at seek: %d\n", 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: %s\n", 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: %s\n", 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.
什么是golang
golang 是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言,其语法与 C语言相近,但并不包括如枚举、异常处理、继承、泛型、断言、虚函数等功能。
以上就是怎么在golang中利用socket断点续传大文件,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
分享题目:怎么在golang中利用socket断点续传大文件
链接分享:http://www.jxjierui.cn/article/jphioj.html


咨询
建站咨询
