-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.go
More file actions
52 lines (44 loc) · 1.02 KB
/
test_client.go
File metadata and controls
52 lines (44 loc) · 1.02 KB
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
package main
import (
"io"
"log"
"os"
"net"
"encoding/binary"
)
func uploadFile(addr, localPath, remoteFilename string) error {
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
defer conn.Close()
// 1. Send filename length
nameBytes := []byte(remoteFilename)
err = binary.Write(conn, binary.LittleEndian, int32(len(nameBytes)))
if err != nil {
return err
}
// 2. Send filename
_, err = conn.Write(nameBytes)
if err != nil {
return err
}
// 3. Send file content
file, err := os.Open(localPath)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(conn, file)
return err
}
func main() {
if len(os.Args) != 4 {
log.Fatal("Usage: go run test_client.go <addr:port> <local_file> <remote_filename>")
}
err := uploadFile(os.Args[1], os.Args[2], os.Args[3])
if err != nil {
log.Fatal("Upload failed:", err)
}
log.Println("Upload successful!")
}