-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (73 loc) · 1.53 KB
/
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
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
package main
import (
"fmt"
"io"
"net"
"os"
)
// Processing States
const (
WAIT_FOR_MSG = iota
IN_MSG
)
func serveConnection(conn net.Conn) {
defer conn.Close()
// sending acknowledgment to the client
if _, err := conn.Write([]byte("*")); err != nil {
fmt.Fprintf(os.Stderr, "Error writing to client: %v\n", err)
return
}
state := WAIT_FOR_MSG
for {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
if err == io.EOF {
break // Client closed the connection
}
fmt.Fprintf(os.Stderr, "Error reading from client: %v\n", err)
return
}
for i := 0; i < n; i++ {
switch state {
case WAIT_FOR_MSG:
if buf[i] == '^' {
state = IN_MSG
fmt.Fprintf(os.Stdout, "In-Message State\n")
}
case IN_MSG:
if buf[i] == '$' {
state = WAIT_FOR_MSG
fmt.Fprintf(os.Stdout, "Wait-For-Message State\n")
}else{
buf[i]++
if _, err := conn.Write(buf[i : i+1]); err != nil {
fmt.Fprintf(os.Stderr, "Error writing to client: %v\n", err)
return
}
}
}
}
}
}
func reportPeerConnected(addr net.Addr) {
fmt.Printf("Peer connected: %s\n", addr.String())
}
func main() {
port := 9090
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
fmt.Fprintf(os.Stderr, "listen error: %v\n", err)
return
}
for {
conn, err := listener.Accept()
if err != nil {
fmt.Fprintf(os.Stderr, "accept error: %v\n", err)
return
}
reportPeerConnected(conn.RemoteAddr())
serveConnection(conn)
fmt.Println("peer done")
}
}