修复tcp传输分界问题

master
兔子 5 years ago
parent dc32f808cf
commit 7d30c2ddc9

@ -146,7 +146,7 @@ func init() {
tcpcmd.AddCommand(tcpsendcmd, tcprecvcmd)
}
var cansend bool = false
var cansend, backsck bool = false, false
var tcpsendcmd = &cobra.Command{
Use: "send",
Short: "通过tcp发送文件",
@ -181,6 +181,8 @@ var tcpsendcmd = &cobra.Command{
data := string(buf[0:n])
if data == "oksend!" {
cansend = true
} else if data == "backsck" {
backsck = true
} else {
fmt.Println(data)
os.Exit(1)
@ -230,13 +232,16 @@ func tcpupload(file, prefix string, mytcp net.Conn) error {
cansend = false
sum := 0
for {
buf := make([]byte, 1048576)
buf := make([]byte, 524288)
n, err := fpsrc.Read(buf)
if err != nil {
if err == io.EOF {
time.Sleep(time.Millisecond * 8)
err = nil
for !backsck {
time.Sleep(time.Millisecond * 150)
mytcp.Write([]byte{1, 9, 9, 6, 1, 1, 2, 7, 6, 6, 6, 1, 1, 2, 7})
}
backsck = false
break
}
fmt.Println(err)
@ -252,7 +257,6 @@ func tcpupload(file, prefix string, mytcp net.Conn) error {
}
fmt.Printf("当前已传输:%f\n\n", 100.000000)
fpsrc.Close()
time.Sleep(time.Millisecond * 8)
return nil
}
@ -343,7 +347,7 @@ var tcprecvcmd = &cobra.Command{
defer conns.Close()
var sum int
for {
buf := make([]byte, 1048576)
buf := make([]byte, 524288)
n, err := conns.Read(buf)
if !canrecv {
@ -377,20 +381,25 @@ var tcprecvcmd = &cobra.Command{
return
}
} else {
if n == 15 {
if n < 524288 && buf[n-1] == byte(7) {
ok := func() bool {
for k, v := range []byte{1, 9, 9, 6, 1, 1, 2, 7, 6, 6, 6, 1, 1, 2, 7} {
if buf[k] != v {
if buf[n-15+k] != v {
return false
}
}
return true
}()
if ok {
if n > 15 {
fpdst.Write(buf[0 : n-15])
}
fmt.Printf("已写入:%d 字节\n", sum)
fmt.Println(name + "接收成功\n")
fpdst.Close()
canrecv = false
sum = 0
conns.Write([]byte("backsck"))
continue
}
}

Loading…
Cancel
Save