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

[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!

I believe the problem is in these lines:

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

n in this case is the number of bytes read, not the value of any of them.

I'd do:

buff := make([]byte, 1)
n,err := conn.Read(buff)
buff = buff[:n]
if len(buff) == 0 {
   //error
}

if(buff[0] != 0){...