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