github.com/devops-filetransfer/sshego@v7.0.4+incompatible/cmd/manual-test-recv/recv.go (about)

     1  /*
     2  a simple test receiver (server) to check that your
     3  direct-tcpip connections are coming through the sshd.
     4  See ../cmd/manual-test-client/client.go for the client part.
     5  */
     6  package main
     7  
     8  import (
     9  	"fmt"
    10  	"net"
    11  )
    12  
    13  func main() {
    14  	lsn, err := net.Listen("tcp", "127.0.0.1:8888")
    15  	panicOn(err)
    16  	fmt.Printf("\n listening on '%s'\n", lsn.Addr())
    17  
    18  	tcpServerConn, err := lsn.Accept()
    19  	panicOn(err)
    20  	//fmt.Printf("%v\n", tcpServerConn)
    21  
    22  	payloadByteCount := 4
    23  	b := make([]byte, payloadByteCount)
    24  	n, err := tcpServerConn.Read(b)
    25  	panicOn(err)
    26  	if n != payloadByteCount {
    27  		panic(fmt.Errorf("read too short! got %v but expected %v", n, payloadByteCount))
    28  	}
    29  	saw := string(b)
    30  
    31  	fmt.Printf("success! server got expected confirmation payload of '%s'\n", saw)
    32  
    33  	// reply back
    34  	n, err = tcpServerConn.Write([]byte("pong"))
    35  	panicOn(err)
    36  	if n != payloadByteCount {
    37  		panic(fmt.Errorf("write too short! got %v but expected %v", n, payloadByteCount))
    38  	}
    39  	tcpServerConn.Close()
    40  	fmt.Printf("replied with 'pong'\n")
    41  }
    42  
    43  func panicOn(err error) {
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  }