...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package main
20
21 import (
22 "fmt"
23 "net"
24 "os"
25
26 "github.com/coreos/go-systemd/v22/activation"
27 )
28
29 func fixListenPid() {
30 if os.Getenv("FIX_LISTEN_PID") != "" {
31
32
33
34 os.Setenv("LISTEN_PID", fmt.Sprintf("%d", os.Getpid()))
35 }
36 }
37
38 func main() {
39 fixListenPid()
40
41 pc, err := activation.PacketConns()
42 if err != nil {
43 panic(err)
44 }
45
46 if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" || os.Getenv("LISTEN_FDNAMES") != "" {
47 panic("Can not unset envs")
48 }
49
50 udp1, ok := pc[0].(*net.UDPConn)
51 if !ok {
52 panic("packetConn 1 not UDP")
53 }
54 udp2, ok := pc[1].(*net.UDPConn)
55 if !ok {
56 panic("packetConn 2 not UDP")
57 }
58
59 _, addr1, err := udp1.ReadFromUDP(nil)
60 if err != nil {
61 panic(err)
62 }
63 _, addr2, err := udp2.ReadFromUDP(nil)
64 if err != nil {
65 panic(err)
66 }
67
68
69 _, err = udp1.WriteToUDP([]byte("Hello world"), addr1)
70 if err != nil {
71 panic(err)
72 }
73 _, err = udp2.WriteToUDP([]byte("Goodbye world"), addr2)
74 if err != nil {
75 panic(err)
76 }
77
78 return
79 }
80
View as plain text