dongzouhe9734 2017-05-23 14:20
浏览 174
已采纳

conn(net.Conn)并不总是写在套接字上

I have done an application with a server and client to send information with a socket TCP

The problem is, if the function zfs.ReceiveSnapshot (in server side) does not return an error (err == nil), conn.Write([]byte("0")) does not work and the client does not receive any byte to continue and it cannot close the connection...

I show you the code of server and client

Server:

package main

import (
    "net"
    "github.com/mistifyio/go-zfs"
)

func main() {
    // Listen for incoming connections
    l, _ := net.Listen("tcp", ":7766")

    // Close the listener when the application closes
    defer l.Close()
    fmt.Println("Listening on port 7766...")
    for {
        // Listen for an incoming connection.
        conn, _ := l.Accept()
        // Handle connections in a new goroutine.
        go handleRequest(conn)
    }
}

// Handles incoming requests
func handleRequest(conn net.Conn) {
    // Receive snapshot
    _, err := zfs.ReceiveSnapshot(conn, "tank/replication")
    if err != nil {
        conn.Write([]byte("1"))
        zfs.ReceiveSnapshot(conn, "tank/replication")
        conn.Close()
    } else {
        conn.Write([]byte("0"))
        conn.Close()
    }
}

Client:

package main

import (
    "net"
    "github.com/mistifyio/go-zfs"
)

func main() {
    conn, _ := net.Dial("tcp", "192.168.99.5:7766")

    for i := 0; i < 1; i++ {
        // Get all snapshots in tank/test
        take, _ := zfs.Snapshots("tank/test")

        // Select snapshots
        snap := take[0].Name
        ds1, _ := zfs.GetDataset(snap)

        // Send first snapshot
        ds1.SendSnapshot(conn)
        defer conn.Close()

        buff := make([]byte, 1024)
        n, _ := conn.Read(buff)

        if n != 0 {
            snap = take[1].Name
            ds2, _ := zfs.GetDataset(snap)
            zfs.SendSnapshotIncremental(conn, ds1, ds2, zfs.IncrementalStream)
            conn.Close()
        }
    }
}

[EDIT]: If ReceiveSnapshots returns an error, conn.Write([]byte ) writes "1", the cliente receives it, execute SendSnapshotIncremental (it does if n != 0) and close the connection in client side... But, if ReceiveSnapshot does not return an error, conn.Write([]byte ) does not write "0" just only I close the connection in server side with ctrl+C

  • 写回答

2条回答 默认 最新

  • doudi2229 2017-05-24 10:19
    关注

    [Edit & Solved]

    Several modifications to solve this...

    Server:

    func handleRequest(conn net.Conn) {
        // Number of snapshots in server side
        take, _ := zfs.Snapshots("tank/replication")
        count := len(take)
        if count < 1 {
            conn.Write([]byte("0"))
        } else {
            conn.Write([]byte("1"))
        }
    
        // Receive snapshot
        zfs.ReceiveSnapshot(conn, "tank/replication")
    }
    

    count returns the number of snapshots that server has, so if there are nothing, the client will execute the case, in other case the default.

    Client:

    func main() {
        // Get the last two snapshots
        take, _ := zfs.Snapshots("tank/test")
        snap := take[0].Name
        ds1, _ := zfs.GetDataset(snap)
        snap = take[1].Name
        ds2, _ := zfs.GetDataset(snap)
    
        // New server connection
        conn, _ := net.Dial("tcp", "192.168.99.5:7766")
    
        // Read data of server side
        buff := bufio.NewReader(conn)
        n, _ := buff.ReadByte()
    
        // Execute the correct case
        switch string(n) {
        case "0":
            ds1.SendSnapshot(conn)
            conn.Close()
        default :
            zfs.SendSnapshotIncremental(conn, ds1, ds2, zfs.IncrementalStream)
            conn.Close()
        }
    }
    

    I don't know if it's so good this solution but it works!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程