blob: e4d732e68a16f326a31082b475ab5ada5ed3b700 [file] [log] [blame]
Andrew Gerrand3e804f92012-02-18 11:48:33 +11001// Copyright 2012 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package net_test
6
7import (
Kevin Burke0df762e2016-11-03 15:34:05 -07008 "fmt"
Andrew Gerrand3e804f92012-02-18 11:48:33 +11009 "io"
10 "log"
11 "net"
12)
13
14func ExampleListener() {
Mikio Hara424b0652017-01-08 17:16:46 +090015 // Listen on TCP port 2000 on all available unicast and
16 // anycast IP addresses of the local system.
Andrew Gerrand3e804f92012-02-18 11:48:33 +110017 l, err := net.Listen("tcp", ":2000")
18 if err != nil {
19 log.Fatal(err)
20 }
Mikio Hara66b797a2013-03-29 15:07:10 +090021 defer l.Close()
Andrew Gerrand3e804f92012-02-18 11:48:33 +110022 for {
Robert Griesemer465b9c32012-10-30 13:38:01 -070023 // Wait for a connection.
Andrew Gerrand3e804f92012-02-18 11:48:33 +110024 conn, err := l.Accept()
25 if err != nil {
26 log.Fatal(err)
27 }
28 // Handle the connection in a new goroutine.
29 // The loop then returns to accepting, so that
30 // multiple connections may be served concurrently.
31 go func(c net.Conn) {
32 // Echo all incoming data.
33 io.Copy(c, c)
34 // Shut down the connection.
35 c.Close()
36 }(conn)
37 }
38}
Kevin Burke0df762e2016-11-03 15:34:05 -070039
Rob Phoenix716761b2017-05-08 22:08:42 +010040func ExampleIPv4() {
41 fmt.Println(net.IPv4(8, 8, 8, 8))
42
43 // Output:
44 // 8.8.8.8
45}
46
47func ExampleParseCIDR() {
Rob Phoenix1e732ca2017-05-09 10:26:08 +010048 ipv4Addr, ipv4Net, err := net.ParseCIDR("192.0.2.1/24")
Rob Phoenix716761b2017-05-08 22:08:42 +010049 if err != nil {
50 log.Fatal(err)
51 }
52 fmt.Println(ipv4Addr)
53 fmt.Println(ipv4Net)
54
55 ipv6Addr, ipv6Net, err := net.ParseCIDR("2001:db8:a0b:12f0::1/32")
56 if err != nil {
57 log.Fatal(err)
58 }
59 fmt.Println(ipv6Addr)
60 fmt.Println(ipv6Net)
61
62 // Output:
63 // 192.0.2.1
Rob Phoenix1e732ca2017-05-09 10:26:08 +010064 // 192.0.2.0/24
Rob Phoenix716761b2017-05-08 22:08:42 +010065 // 2001:db8:a0b:12f0::1
66 // 2001:db8::/32
67}
68
Rob Phoenix4bf6c562017-05-19 15:18:17 +010069func ExampleParseIP() {
70 fmt.Println(net.ParseIP("192.0.2.1"))
71 fmt.Println(net.ParseIP("2001:db8::68"))
72 fmt.Println(net.ParseIP("192.0.2"))
73
74 // Output:
75 // 192.0.2.1
76 // 2001:db8::68
77 // <nil>
78}
79
80func ExampleIP_DefaultMask() {
81 ip := net.ParseIP("192.0.2.1")
82 fmt.Println(ip.DefaultMask())
83
84 // Output:
85 // ffffff00
86}
87
88func ExampleIP_Mask() {
89 ipv4Addr := net.ParseIP("192.0.2.1")
90 // This mask corresponds to a /24 subnet for IPv4.
91 ipv4Mask := net.CIDRMask(24, 32)
92 fmt.Println(ipv4Addr.Mask(ipv4Mask))
93
94 ipv6Addr := net.ParseIP("2001:db8:a0b:12f0::1")
95 // This mask corresponds to a /32 subnet for IPv6.
96 ipv6Mask := net.CIDRMask(32, 128)
97 fmt.Println(ipv6Addr.Mask(ipv6Mask))
98
99 // Output:
100 // 192.0.2.0
101 // 2001:db8::
102}
103
Kevin Burke0df762e2016-11-03 15:34:05 -0700104func ExampleCIDRMask() {
Mikio Harab8d56fd2016-11-17 12:18:39 +0900105 // This mask corresponds to a /31 subnet for IPv4.
Kevin Burke0df762e2016-11-03 15:34:05 -0700106 fmt.Println(net.CIDRMask(31, 32))
107
Mikio Harab8d56fd2016-11-17 12:18:39 +0900108 // This mask corresponds to a /64 subnet for IPv6.
Kevin Burke0df762e2016-11-03 15:34:05 -0700109 fmt.Println(net.CIDRMask(64, 128))
Mikio Harab8d56fd2016-11-17 12:18:39 +0900110
111 // Output:
112 // fffffffe
Kevin Burke0df762e2016-11-03 15:34:05 -0700113 // ffffffffffffffff0000000000000000
114}
Rob Phoenix716761b2017-05-08 22:08:42 +0100115
116func ExampleIPv4Mask() {
117 fmt.Println(net.IPv4Mask(255, 255, 255, 0))
118
119 // Output:
120 // ffffff00
121}
Adam Medzinski645d4722018-05-18 16:54:30 +0200122
123func ExampleUDPConn_WriteTo() {
Mikio Hara0436b162018-06-25 02:08:52 +0900124 // Unlike Dial, ListenPacket creates a connection without any
125 // association with peers.
Adam Medzinski645d4722018-05-18 16:54:30 +0200126 conn, err := net.ListenPacket("udp", ":0")
127 if err != nil {
128 log.Fatal(err)
129 }
130 defer conn.Close()
131
Mikio Hara7d704a92018-05-30 12:52:49 +0900132 dst, err := net.ResolveUDPAddr("udp", "192.0.2.1:2000")
Adam Medzinski645d4722018-05-18 16:54:30 +0200133 if err != nil {
134 log.Fatal(err)
135 }
136
Mikio Hara0436b162018-06-25 02:08:52 +0900137 // The connection can write data to the desired address.
Adam Medzinski645d4722018-05-18 16:54:30 +0200138 _, err = conn.WriteTo([]byte("data"), dst)
139 if err != nil {
140 log.Fatal(err)
141 }
142}