-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathexample_listen_test.go
43 lines (36 loc) · 1 KB
/
example_listen_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
package raknet_test
import (
"github.com/sandertv/go-raknet"
)
func ExampleListen() {
const address = ":19132"
// Start listening on an address.
l, err := raknet.Listen(address)
if err != nil {
panic(err)
}
for {
// Accept a new connection from the Listener. Accept will only return an error if the Listener is
// closed. (So only after a call to Listener.Close.)
conn, err := l.Accept()
if err != nil {
return
}
// Read a packet from the connection accepted.
p := make([]byte, 1500)
n, err := conn.Read(p)
if err != nil {
panic("error reading packet from " + conn.RemoteAddr().String() + ": " + err.Error())
}
p = p[:n]
// Write a packet to the connection.
data := []byte("Hello World!")
if _, err := conn.Write(data); err != nil {
panic("error writing packet to " + conn.RemoteAddr().String() + ": " + err.Error())
}
// Close the connection after you're done with it.
if err := conn.Close(); err != nil {
panic("error closing connection: " + err.Error())
}
}
}