-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
76 lines (63 loc) · 1.52 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"io"
"log"
"net"
"time"
)
const (
// The port to server the
PORT = ":8082"
)
func main() {
// Resolves the address of the server port and listens
addr, err := net.ResolveTCPAddr("tcp", "35.2.213.76"+PORT)
checkErr(err)
listener, err := net.ListenTCP("tcp", addr)
checkErr(err)
log.Println("Server started.")
// Indefinitely handle connections
for {
newConn, err := listener.AcceptTCP()
checkErr(err)
logClientJoined(newConn)
checkErr(err)
// Handle connection in a new goroutine
go handleConn(newConn)
}
}
// REQUIRES: conn is a pointer to a valid, open TCP connection
// MODIFIES: conn
// EFFECTS: Writes a sample message to the connection.
func handleConn(conn *net.TCPConn) {
for {
connIsClosed(conn)
sampleMessage := []byte("Hello!\n")
_, err := conn.Write(sampleMessage)
checkErr(err)
time.Sleep(1000 * time.Millisecond)
}
}
// REQUIRES: conn is a pointer to a valid, open TCP connection
// EFFECTS: Logs the event where a client joins.
func logClientJoined(conn *net.TCPConn) {
log.Println("server.go: Client joined from %s", conn.RemoteAddr())
}
// EFFECTS: Handles any non-nil errors by printing them.
func checkErr(err error) {
if err != nil {
log.Println("Error: server.go: %s", err.Error())
}
}
func connIsClosed(c *net.TCPConn) {
c.SetReadDeadline(time.Now())
var one []byte
if _, err := c.Read(one); err == io.EOF {
log.Println("Client disconnect: %s", c.RemoteAddr())
c.Close()
c = nil
} else {
var zero time.Time
c.SetReadDeadline(zero)
}
}