-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlave3.go
74 lines (65 loc) · 1.43 KB
/
Slave3.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
package main
import (
"fmt"
"net"
"os"
"strconv"
)
func main() {
// Connect to the master server
conn, err := net.Dial("tcp", "192.168.43.124:9090") // Replace with the master's IP address
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
// Send the slave ID to the master
slaveID := 3// Replace with your desired slave ID
_, err = conn.Write([]byte(strconv.Itoa(slaveID)))
if err != nil {
fmt.Println(err)
return
}
// Send commands to the master and read responses
//for {
var cmd string = "192.168.43.161" // Replace this with your device's IP address
_, err = conn.Write([]byte(cmd))
if err != nil {
fmt.Println(err)
return
}
ln, err := net.Listen("tcp", ":9090")
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Slave " + string(slaveID) + " waiting")
conn1, err := ln.Accept()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Client Connected")
}
buf := make([]byte, 1024)
n, err := conn1.Read(buf)
if err != nil {
fmt.Println(err)
return
}
file := (string(buf[:n]))
// read file path
filePath := file // The path of the file that the client wants
// Read the contents of the file
contentBytes, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading the file:", err)
return
}
// Convert the byte slice to a string
var msg string = string(contentBytes)
_, err = conn1.Write([]byte(msg))
if err != nil {
fmt.Println(err)
return
}
}