-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
98 lines (81 loc) · 1.75 KB
/
client.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
package client
import (
"bufio"
"fmt"
"net"
"os"
"regexp"
"strconv"
// mpc "github.com/Aasif-Javid/TSS-comm/binance"
)
var SendChan = make(chan string)
var ReceiveChan = make(chan string)
func Client() {
var host = "172.20.10.4"
var port = 8000
dest := host + ":" + strconv.Itoa(port)
fmt.Printf("Connecting to %s...\n", dest)
conn, err := net.Dial("tcp", dest)
if err != nil {
fmt.Println("Error connecting:", err.Error())
os.Exit(1)
}
// logger:=mpc.Logger
// party:=mpc.
go sendMessages(conn, SendChan)
go receiveMessages(conn, ReceiveChan)
msg := "hello ths is client "
SendChan <- msg
for {
select {
case msg := <-ReceiveChan:
fmt.Println("Received:", msg)
}
}
}
func sendMsg(msg []byte, isBroadcast bool, to uint16) {
message := string(msg)
SendChan <- message
fmt.Println("Message passed to network sending function")
}
func sendMessages(conn net.Conn, sendChan <-chan string) {
for {
text, ok := <-sendChan
fmt.Println("Preparing to send")
if !ok {
fmt.Println("Channel closed, stopping message sending.")
break
}
_, err := conn.Write([]byte(text + "\n")) // Add a newline at the end of each message
if err != nil {
fmt.Println("Error writing to stream.")
break
}
}
}
func receiveMessages(conn net.Conn, receiveChan chan<- string) {
scanner := bufio.NewScanner(conn)
for {
ok := scanner.Scan()
if !ok {
fmt.Println("Reached EOF on server connection.")
break
}
text := scanner.Text()
receiveChan <- text
}
}
func handleCommands(text string) bool {
r, err := regexp.Compile("^%.*%$")
if err == nil {
if r.MatchString(text) {
switch {
case text == "%quit%":
fmt.Println("\b\bServer is leaving. Hanging up.")
os.Exit(0)
}
return true
}
}
return false
}