-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.go
65 lines (61 loc) · 1.4 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
package main
import (
"fmt"
"net"
"os"
"strconv"
)
func main() {
var port string
var buffsize int
var exit_message string
if os.Args[1] != "" {
port = ":" + os.Args[1]
}
if os.Args[2] != "" {
number, _ := strconv.Atoi(os.Args[2])
buffsize = number
}
if os.Args[3] != "" {
exit = number
}
fmt.Println("Server Starting on PORT " + port)
address, err := net.ResolveTCPAddr("tcp4", port)
checkError(err)
ss, err := net.ListenTCP("tcp", address)
checkError(err)
for {
conn, err := ss.AcceptTCP()
if err != nil {
continue
}
go handler(conn,buffsize,exit_message)
}
}
func handler(conn * net.TCPConn, buffsize int, exit_message string) {
data := make([]byte, buffsize)
_ ,err := conn.Read(data)
message := string(data[:buffsize])
if err != nil {
fmt.Fprintf(os.Stderr, "Client Error: %s", err.Error())
goto End
}
fmt.Println(message)
for message != exit_message {
data = make([]byte, buffsize)
_ ,err := conn.Read(data)
if err != nil {
fmt.Fprintf(os.Stderr, "Client Disconnected %s", err.Error())
goto End
}
message = string(data[:buffsize])
fmt.Println(message)
}
End: conn.Close();
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
os.Exit(1)
}
}