-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.go
82 lines (72 loc) · 1.58 KB
/
sender.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
package main
import (
"bufio"
"fmt"
"io"
"net"
"os"
)
func main() {
if len(os.Args) != 4 {
fmt.Println("Usage: sender <server_ip> <session_id> <file_path>")
os.Exit(1)
}
serverIP := os.Args[1]
sessionID := os.Args[2]
filePath := os.Args[3]
conn, err := net.Dial("tcp", serverIP+":8080")
if err != nil {
fmt.Printf("Error connecting to server: %v\n", err)
os.Exit(1)
}
defer conn.Close()
// Send client type and session ID
_, err = fmt.Fprintf(conn, "SENDER\n%s\n", sessionID)
if err != nil {
fmt.Printf("Error sending session ID: %v\n", err)
os.Exit(1)
}
// Wait for READY signal
scanner := bufio.NewScanner(conn)
if scanner.Scan() {
response := scanner.Text()
if response != "READY" {
fmt.Printf("Error: %s\n", response)
os.Exit(1)
}
fmt.Println("Receiver is ready. Starting file transfer...")
} else {
fmt.Println("No response from server.")
os.Exit(1)
}
// Open the file to send
file, err := os.Open(filePath)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
os.Exit(1)
}
defer file.Close()
fmt.Printf("Sending file: %s\n", filePath)
buffer := make([]byte, 4096)
totalBytesSent := 0
for {
n, err := file.Read(buffer)
if n > 0 {
_, writeErr := conn.Write(buffer[:n])
if writeErr != nil {
fmt.Printf("Error sending data: %v\n", writeErr)
break
}
totalBytesSent += n
fmt.Printf("Sent %d bytes to server.\n", n)
}
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
break
}
}
fmt.Printf("File sent successfully (%d bytes).\n", totalBytesSent)
}