...

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  	"context"
		 9  	"fmt"
		10  	"io"
		11  	"log"
		12  	"net"
		13  	"time"
		14  )
		15  
		16  func ExampleListener() {
		17  	// Listen on TCP port 2000 on all available unicast and
		18  	// anycast IP addresses of the local system.
		19  	l, err := net.Listen("tcp", ":2000")
		20  	if err != nil {
		21  		log.Fatal(err)
		22  	}
		23  	defer l.Close()
		24  	for {
		25  		// Wait for a connection.
		26  		conn, err := l.Accept()
		27  		if err != nil {
		28  			log.Fatal(err)
		29  		}
		30  		// Handle the connection in a new goroutine.
		31  		// The loop then returns to accepting, so that
		32  		// multiple connections may be served concurrently.
		33  		go func(c net.Conn) {
		34  			// Echo all incoming data.
		35  			io.Copy(c, c)
		36  			// Shut down the connection.
		37  			c.Close()
		38  		}(conn)
		39  	}
		40  }
		41  
		42  func ExampleDialer() {
		43  	var d net.Dialer
		44  	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
		45  	defer cancel()
		46  
		47  	conn, err := d.DialContext(ctx, "tcp", "localhost:12345")
		48  	if err != nil {
		49  		log.Fatalf("Failed to dial: %v", err)
		50  	}
		51  	defer conn.Close()
		52  
		53  	if _, err := conn.Write([]byte("Hello, World!")); err != nil {
		54  		log.Fatal(err)
		55  	}
		56  }
		57  
		58  func ExampleDialer_unix() {
		59  	// DialUnix does not take a context.Context parameter. This example shows
		60  	// how to dial a Unix socket with a Context. Note that the Context only
		61  	// applies to the dial operation; it does not apply to the connection once
		62  	// it has been established.
		63  	var d net.Dialer
		64  	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
		65  	defer cancel()
		66  
		67  	d.LocalAddr = nil // if you have a local addr, add it here
		68  	raddr := net.UnixAddr{Name: "/path/to/unix.sock", Net: "unix"}
		69  	conn, err := d.DialContext(ctx, "unix", raddr.String())
		70  	if err != nil {
		71  		log.Fatalf("Failed to dial: %v", err)
		72  	}
		73  	defer conn.Close()
		74  	if _, err := conn.Write([]byte("Hello, socket!")); err != nil {
		75  		log.Fatal(err)
		76  	}
		77  }
		78  
		79  func ExampleIPv4() {
		80  	fmt.Println(net.IPv4(8, 8, 8, 8))
		81  
		82  	// Output:
		83  	// 8.8.8.8
		84  }
		85  
		86  func ExampleParseCIDR() {
		87  	ipv4Addr, ipv4Net, err := net.ParseCIDR("192.0.2.1/24")
		88  	if err != nil {
		89  		log.Fatal(err)
		90  	}
		91  	fmt.Println(ipv4Addr)
		92  	fmt.Println(ipv4Net)
		93  
		94  	ipv6Addr, ipv6Net, err := net.ParseCIDR("2001:db8:a0b:12f0::1/32")
		95  	if err != nil {
		96  		log.Fatal(err)
		97  	}
		98  	fmt.Println(ipv6Addr)
		99  	fmt.Println(ipv6Net)
	 100  
	 101  	// Output:
	 102  	// 192.0.2.1
	 103  	// 192.0.2.0/24
	 104  	// 2001:db8:a0b:12f0::1
	 105  	// 2001:db8::/32
	 106  }
	 107  
	 108  func ExampleParseIP() {
	 109  	fmt.Println(net.ParseIP("192.0.2.1"))
	 110  	fmt.Println(net.ParseIP("2001:db8::68"))
	 111  	fmt.Println(net.ParseIP("192.0.2"))
	 112  
	 113  	// Output:
	 114  	// 192.0.2.1
	 115  	// 2001:db8::68
	 116  	// <nil>
	 117  }
	 118  
	 119  func ExampleIP_DefaultMask() {
	 120  	ip := net.ParseIP("192.0.2.1")
	 121  	fmt.Println(ip.DefaultMask())
	 122  
	 123  	// Output:
	 124  	// ffffff00
	 125  }
	 126  
	 127  func ExampleIP_Mask() {
	 128  	ipv4Addr := net.ParseIP("192.0.2.1")
	 129  	// This mask corresponds to a /24 subnet for IPv4.
	 130  	ipv4Mask := net.CIDRMask(24, 32)
	 131  	fmt.Println(ipv4Addr.Mask(ipv4Mask))
	 132  
	 133  	ipv6Addr := net.ParseIP("2001:db8:a0b:12f0::1")
	 134  	// This mask corresponds to a /32 subnet for IPv6.
	 135  	ipv6Mask := net.CIDRMask(32, 128)
	 136  	fmt.Println(ipv6Addr.Mask(ipv6Mask))
	 137  
	 138  	// Output:
	 139  	// 192.0.2.0
	 140  	// 2001:db8::
	 141  }
	 142  
	 143  func ExampleCIDRMask() {
	 144  	// This mask corresponds to a /31 subnet for IPv4.
	 145  	fmt.Println(net.CIDRMask(31, 32))
	 146  
	 147  	// This mask corresponds to a /64 subnet for IPv6.
	 148  	fmt.Println(net.CIDRMask(64, 128))
	 149  
	 150  	// Output:
	 151  	// fffffffe
	 152  	// ffffffffffffffff0000000000000000
	 153  }
	 154  
	 155  func ExampleIPv4Mask() {
	 156  	fmt.Println(net.IPv4Mask(255, 255, 255, 0))
	 157  
	 158  	// Output:
	 159  	// ffffff00
	 160  }
	 161  
	 162  func ExampleUDPConn_WriteTo() {
	 163  	// Unlike Dial, ListenPacket creates a connection without any
	 164  	// association with peers.
	 165  	conn, err := net.ListenPacket("udp", ":0")
	 166  	if err != nil {
	 167  		log.Fatal(err)
	 168  	}
	 169  	defer conn.Close()
	 170  
	 171  	dst, err := net.ResolveUDPAddr("udp", "192.0.2.1:2000")
	 172  	if err != nil {
	 173  		log.Fatal(err)
	 174  	}
	 175  
	 176  	// The connection can write data to the desired address.
	 177  	_, err = conn.WriteTo([]byte("data"), dst)
	 178  	if err != nil {
	 179  		log.Fatal(err)
	 180  	}
	 181  }
	 182  

View as plain text