...
Run Format

Source file src/net/example_test.go

Documentation: net

     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 (
     8  	"fmt"
     9  	"io"
    10  	"log"
    11  	"net"
    12  )
    13  
    14  func ExampleListener() {
    15  	// Listen on TCP port 2000 on all available unicast and
    16  	// anycast IP addresses of the local system.
    17  	l, err := net.Listen("tcp", ":2000")
    18  	if err != nil {
    19  		log.Fatal(err)
    20  	}
    21  	defer l.Close()
    22  	for {
    23  		// Wait for a connection.
    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  }
    39  
    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() {
    48  	ipv4Addr, ipv4Net, err := net.ParseCIDR("192.0.2.1/24")
    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
    64  	// 192.0.2.0/24
    65  	// 2001:db8:a0b:12f0::1
    66  	// 2001:db8::/32
    67  }
    68  
    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  
   104  func ExampleCIDRMask() {
   105  	// This mask corresponds to a /31 subnet for IPv4.
   106  	fmt.Println(net.CIDRMask(31, 32))
   107  
   108  	// This mask corresponds to a /64 subnet for IPv6.
   109  	fmt.Println(net.CIDRMask(64, 128))
   110  
   111  	// Output:
   112  	// fffffffe
   113  	// ffffffffffffffff0000000000000000
   114  }
   115  
   116  func ExampleIPv4Mask() {
   117  	fmt.Println(net.IPv4Mask(255, 255, 255, 0))
   118  
   119  	// Output:
   120  	// ffffff00
   121  }
   122  
   123  func ExampleUDPConn_WriteTo() {
   124  	// Unlike Dial, ListenPacket creates a connection without any
   125  	// association with peers.
   126  	conn, err := net.ListenPacket("udp", ":0")
   127  	if err != nil {
   128  		log.Fatal(err)
   129  	}
   130  	defer conn.Close()
   131  
   132  	dst, err := net.ResolveUDPAddr("udp", "192.0.2.1:2000")
   133  	if err != nil {
   134  		log.Fatal(err)
   135  	}
   136  
   137  	// The connection can write data to the desired address.
   138  	_, err = conn.WriteTo([]byte("data"), dst)
   139  	if err != nil {
   140  		log.Fatal(err)
   141  	}
   142  }
   143  

View as plain text