-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (39 loc) · 921 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
49
package main
import (
"flag"
"log"
"net"
)
var (
addr string
port string
)
func main() {
flag.StringVar(&port, "port", "", "Server listen port.")
flag.StringVar(&addr, "addr", "127.0.0.1", "Server listen address.")
flag.Parse()
if port == "" {
log.Fatalf("argument -port, server listen port, is required")
}
log.Printf("Starting TPC server on '%s:%s'", addr, port)
// Create TCP listener.
l, err := net.Listen("tcp", addr+":"+port)
if err != nil {
log.Fatalf("listener failed: %s", err)
}
defer l.Close()
for {
// Accept new connections.
c, err := l.Accept()
if err != nil {
log.Fatalf("unable to accept a new connection: %s", err)
}
log.Printf("TCP connection received from: %s", c.RemoteAddr())
// Use SetLinger to force close the connection.
err = c.(*net.TCPConn).SetLinger(0)
if err != nil {
log.Printf("failed to reset connection: %s", err)
}
c.Close()
}
}