-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
main.go
48 lines (42 loc) · 917 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"bufio"
"fmt"
"log"
"net"
)
func handle(conn net.Conn) {
defer conn.Close()
// NewScanner returns a new Scanner to read from r.
// The split function defaults to ScanLines.
scanner := bufio.NewScanner(conn)
// Scan advances the Scanner to the next token, which will then be
// available through the Bytes or Text method.
for scanner.Scan() {
// Text returns the most recent token generated by a call to Scan
// as a newly allocated string holding its bytes.
ln := scanner.Text()
fmt.Println(ln)
fmt.Printf("TYPE: %T\n", ln)
ln = fmt.Sprint("FROM SERVER: " + ln)
fmt.Fprintln(conn, ln)
}
}
func main() {
li, err := net.Listen("tcp", ":9000")
if err != nil {
log.Fatalln(err)
}
defer li.Close()
for {
conn, err := li.Accept()
if err != nil {
log.Fatalln(err)
}
handle(conn)
}
}
/*
start main.go (go run main.go) then ...
telnet localhost 9000
*/