-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvrTcp.go
50 lines (44 loc) · 1.24 KB
/
svrTcp.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
49
50
package main
import (
"bufio"
"fmt"
"net"
"strings"
)
func handClose(c net.Conn){
fmt.Printf("%+v Close! : %v\n",c,c.RemoteAddr().String())
c.Close()
}
func handConn(c net.Conn){
defer handClose(c)
fmt.Printf("Handle new connection:%v\n",c);
for{
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println("Server read error ",err)
return
}
if strings.TrimSpace(string(netData)) == "STOP" {
fmt.Println("Exiting TCP server!")
return
}
fmt.Print("Server receive : ", string(netData))
replay := c.RemoteAddr().String() + "\n"
c.Write([]byte(replay))
}
}
func svrStart(port string){
l, err := net.Listen("tcp", ":"+ port)
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
fmt.Println("Tcp server start success")
for{
c, err := l.Accept()
if err == nil {
go handConn(c)
}
}//end for
}