Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 1 | // 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 | |
| 5 | package net_test |
| 6 | |
| 7 | import ( |
Kevin Burke | 0df762e | 2016-11-03 15:34:05 -0700 | [diff] [blame] | 8 | "fmt" |
Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 9 | "io" |
| 10 | "log" |
| 11 | "net" |
| 12 | ) |
| 13 | |
| 14 | func ExampleListener() { |
Mikio Hara | 424b065 | 2017-01-08 17:16:46 +0900 | [diff] [blame] | 15 | // Listen on TCP port 2000 on all available unicast and |
| 16 | // anycast IP addresses of the local system. |
Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 17 | l, err := net.Listen("tcp", ":2000") |
| 18 | if err != nil { |
| 19 | log.Fatal(err) |
| 20 | } |
Mikio Hara | 66b797a | 2013-03-29 15:07:10 +0900 | [diff] [blame] | 21 | defer l.Close() |
Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 22 | for { |
Robert Griesemer | 465b9c3 | 2012-10-30 13:38:01 -0700 | [diff] [blame] | 23 | // Wait for a connection. |
Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 24 | 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 Burke | 0df762e | 2016-11-03 15:34:05 -0700 | [diff] [blame] | 39 | |
Rob Phoenix | 716761b | 2017-05-08 22:08:42 +0100 | [diff] [blame] | 40 | func ExampleIPv4() { |
| 41 | fmt.Println(net.IPv4(8, 8, 8, 8)) |
| 42 | |
| 43 | // Output: |
| 44 | // 8.8.8.8 |
| 45 | } |
| 46 | |
| 47 | func ExampleParseCIDR() { |
Rob Phoenix | 1e732ca | 2017-05-09 10:26:08 +0100 | [diff] [blame] | 48 | ipv4Addr, ipv4Net, err := net.ParseCIDR("192.0.2.1/24") |
Rob Phoenix | 716761b | 2017-05-08 22:08:42 +0100 | [diff] [blame] | 49 | 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 Phoenix | 1e732ca | 2017-05-09 10:26:08 +0100 | [diff] [blame] | 64 | // 192.0.2.0/24 |
Rob Phoenix | 716761b | 2017-05-08 22:08:42 +0100 | [diff] [blame] | 65 | // 2001:db8:a0b:12f0::1 |
| 66 | // 2001:db8::/32 |
| 67 | } |
| 68 | |
Rob Phoenix | 4bf6c56 | 2017-05-19 15:18:17 +0100 | [diff] [blame] | 69 | func 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 | |
| 80 | func ExampleIP_DefaultMask() { |
| 81 | ip := net.ParseIP("192.0.2.1") |
| 82 | fmt.Println(ip.DefaultMask()) |
| 83 | |
| 84 | // Output: |
| 85 | // ffffff00 |
| 86 | } |
| 87 | |
| 88 | func 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 Burke | 0df762e | 2016-11-03 15:34:05 -0700 | [diff] [blame] | 104 | func ExampleCIDRMask() { |
Mikio Hara | b8d56fd | 2016-11-17 12:18:39 +0900 | [diff] [blame] | 105 | // This mask corresponds to a /31 subnet for IPv4. |
Kevin Burke | 0df762e | 2016-11-03 15:34:05 -0700 | [diff] [blame] | 106 | fmt.Println(net.CIDRMask(31, 32)) |
| 107 | |
Mikio Hara | b8d56fd | 2016-11-17 12:18:39 +0900 | [diff] [blame] | 108 | // This mask corresponds to a /64 subnet for IPv6. |
Kevin Burke | 0df762e | 2016-11-03 15:34:05 -0700 | [diff] [blame] | 109 | fmt.Println(net.CIDRMask(64, 128)) |
Mikio Hara | b8d56fd | 2016-11-17 12:18:39 +0900 | [diff] [blame] | 110 | |
| 111 | // Output: |
| 112 | // fffffffe |
Kevin Burke | 0df762e | 2016-11-03 15:34:05 -0700 | [diff] [blame] | 113 | // ffffffffffffffff0000000000000000 |
| 114 | } |
Rob Phoenix | 716761b | 2017-05-08 22:08:42 +0100 | [diff] [blame] | 115 | |
| 116 | func ExampleIPv4Mask() { |
| 117 | fmt.Println(net.IPv4Mask(255, 255, 255, 0)) |
| 118 | |
| 119 | // Output: |
| 120 | // ffffff00 |
| 121 | } |
Adam Medzinski | 645d472 | 2018-05-18 16:54:30 +0200 | [diff] [blame] | 122 | |
| 123 | func ExampleUDPConn_WriteTo() { |
Mikio Hara | 0436b16 | 2018-06-25 02:08:52 +0900 | [diff] [blame] | 124 | // Unlike Dial, ListenPacket creates a connection without any |
| 125 | // association with peers. |
Adam Medzinski | 645d472 | 2018-05-18 16:54:30 +0200 | [diff] [blame] | 126 | conn, err := net.ListenPacket("udp", ":0") |
| 127 | if err != nil { |
| 128 | log.Fatal(err) |
| 129 | } |
| 130 | defer conn.Close() |
| 131 | |
Mikio Hara | 7d704a9 | 2018-05-30 12:52:49 +0900 | [diff] [blame] | 132 | dst, err := net.ResolveUDPAddr("udp", "192.0.2.1:2000") |
Adam Medzinski | 645d472 | 2018-05-18 16:54:30 +0200 | [diff] [blame] | 133 | if err != nil { |
| 134 | log.Fatal(err) |
| 135 | } |
| 136 | |
Mikio Hara | 0436b16 | 2018-06-25 02:08:52 +0900 | [diff] [blame] | 137 | // The connection can write data to the desired address. |
Adam Medzinski | 645d472 | 2018-05-18 16:54:30 +0200 | [diff] [blame] | 138 | _, err = conn.WriteTo([]byte("data"), dst) |
| 139 | if err != nil { |
| 140 | log.Fatal(err) |
| 141 | } |
| 142 | } |