forked from keybase/npipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_windows_test.go
53 lines (47 loc) · 945 Bytes
/
example_windows_test.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
49
50
51
52
53
package npipe_test
import (
"bufio"
"fmt"
"net"
"github.com/elmeyer/npipe"
)
// Use Dial to connect to a server and read messages from it.
func ExampleDial() {
conn, err := npipe.Dial(`\\.\pipe\mypipe`)
if err != nil {
// handle error
}
if _, err := fmt.Fprintln(conn, "Hi server!"); err != nil {
// handle error
}
r := bufio.NewReader(conn)
msg, err := r.ReadString('\n')
if err != nil {
// handle eror
}
fmt.Println(msg)
}
// Use Listen to start a server, and accept connections with Accept().
func ExampleListen() {
ln, err := npipe.Listen(`\\.\pipe\mypipe`, 1)
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
continue
}
// handle connection like any other net.Conn
go func(conn net.Conn) {
r := bufio.NewReader(conn)
msg, err := r.ReadString('\n')
if err != nil {
// handle error
return
}
fmt.Println(msg)
}(conn)
}
}