-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
99 lines (80 loc) · 1.8 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"fmt"
"log"
"net"
"runtime"
"strings"
"time"
)
//handler function implemented from net package
func handleConnection(conn net.Conn) {
for {
message, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Fatal(err)
return
}
fmt.Print("Message Received is:", message)
//close client out if message is END
endMsg := strings.TrimSpace(string(message))
if endMsg == "END" {
break
}
newmessage := strings.ToUpper(message)
// send new string back to client
conn.Write([]byte(newmessage + "\n"))
}
conn.Close()
}
func main() {
numThreads := loopInput2()
runtime.GOMAXPROCS(numThreads)
fmt.Println("Server is running")
time1 := time.Now()
// create server with net listen function
ln, err := net.Listen("tcp", "127.0.0.1:4040")
if err != nil {
log.Fatal(err)
return
}
connClients := 0
// run connection function as routine
for {
conn, err := ln.Accept()
connClients++
fmt.Println(connClients)
log.Println(time.Since(time1))
if err != nil {
log.Fatal(err)
return
}
go handleConnection(conn)
}
log.Println(time.Since(time1))
fmt.Println("Server closed")
}
func loopInput2() int {
needInput := true
input := []int{0}
for needInput {
procsNum, updateNeedInput := askInput2()
input[0] = procsNum
needInput = updateNeedInput
}
return input[0]
}
func askInput2() (int, bool) {
fmt.Println("This program will setup the server")
fmt.Println("Input the amount of processors to use")
var numProcs int
_, err := fmt.Scanln(&numProcs)
if err != nil {
fmt.Println("Invalid number of threads. Try again with an integer")
return 0, true
}
fmt.Println("Starting the server using a GOMAXPROCS number of ", numProcs, " .")
fmt.Println("----------------------------------------------")
return numProcs, false
}