day10协程 实例 echo
parent
29620426c2
commit
e31d5c975f
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func echo(c net.Conn, shout string, delay time.Duration) {
|
||||
fmt.Fprintln(c, "\t", strings.ToUpper(shout))
|
||||
time.Sleep(delay)
|
||||
fmt.Fprintln(c, "\t", shout)
|
||||
time.Sleep(delay)
|
||||
fmt.Fprintln(c, "\t", strings.ToLower(shout))
|
||||
}
|
||||
|
||||
func handleConn1(c net.Conn) {
|
||||
input := bufio.NewScanner(c)
|
||||
for input.Scan() {
|
||||
echo(c, input.Text(), 1*time.Second)
|
||||
}
|
||||
// NOTE: ignoring potential errors from input.Err()
|
||||
c.Close()
|
||||
}
|
||||
func main() {
|
||||
conn, err := net.Dial("tcp", "localhost:8000")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
go mustCopy(os.Stdout, conn)
|
||||
mustCopy(conn, os.Stdin)
|
||||
}
|
Loading…
Reference in New Issue