-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp-server.go
52 lines (46 loc) · 1.01 KB
/
tcp-server.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
51
52
package main
import (
"bufio"
"fmt"
"net"
"os"
"github.com/kaiya/goutils/tinyrpc/rpc"
)
const (
CONN_HOST = "localhost"
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
func main() {
// Listen for incoming connections.
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
}
// Handles incoming requests.
func handleRequest(conn net.Conn) {
defer conn.Close()
bufconn := bufio.NewReader(conn)
msg, err := rpc.DecodePacket(bufconn)
if err != nil {
fmt.Printf("decode packet error:%s\n", err)
return
}
fmt.Println("got msg len: ", len(msg))
fmt.Println("msg: ", string(msg))
}