blob: 6f2f9074c1f627687ea7895b003c348bdb832fbb [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 (
8 "io"
9 "log"
10 "net"
11)
12
13func ExampleListener() {
14 // Listen on TCP port 2000 on all interfaces.
15 l, err := net.Listen("tcp", ":2000")
16 if err != nil {
17 log.Fatal(err)
18 }
Mikio Hara66b797a2013-03-29 15:07:10 +090019 defer l.Close()
Andrew Gerrand3e804f92012-02-18 11:48:33 +110020 for {
Robert Griesemer465b9c32012-10-30 13:38:01 -070021 // Wait for a connection.
Andrew Gerrand3e804f92012-02-18 11:48:33 +110022 conn, err := l.Accept()
23 if err != nil {
24 log.Fatal(err)
25 }
26 // Handle the connection in a new goroutine.
27 // The loop then returns to accepting, so that
28 // multiple connections may be served concurrently.
29 go func(c net.Conn) {
30 // Echo all incoming data.
31 io.Copy(c, c)
32 // Shut down the connection.
33 c.Close()
34 }(conn)
35 }
36}