forked from swoole/benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.go
41 lines (35 loc) · 800 Bytes
/
tcp.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
package main
import (
"bufio"
"fmt"
"net"
"runtime"
)
func Echo(c net.Conn) {
defer c.Close()
for {
line, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
//fmt.Printf("Failure to read:%s\n", err.Error())
return
}
_, err = c.Write([]byte(line))
if err != nil {
//fmt.Printf("Failure to write: %s\n", err.Error())
return
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
fmt.Printf("Server is ready...\n")
l, err := net.Listen("tcp", ":8053")
if err != nil {
fmt.Printf("Failure to listen: %s\n", err.Error())
}
for {
if c, err := l.Accept(); err == nil {
go Echo(c) //new thread
}
}
}