-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
69 lines (60 loc) · 1.67 KB
/
Copy pathclient.cpp
File metadata and controls
69 lines (60 loc) · 1.67 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
bool read_full(int fd, char* buf, int n) {
int total = 0;
while (total < n) {
int rv = read(fd, buf + total, n - total);
if (rv <= 0) return false;
total += rv;
}
return true;
}
bool write_all(int fd, const char* buf, int n) {
int total = 0;
while (total < n) {
int rv = write(fd, buf + total, n - total);
if (rv <= 0) return false;
total += rv;
}
return true;
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: ./client COMMAND [args...]\n";
return 1;
}
// Combine terminal arguments into one command string
std::string command = argv[1];
for (int i = 2; i < argc; ++i) {
command += " ";
command += argv[i];
}
// Connect to the server
int fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
std::cerr << "Connection failed\n";
return 1;
}
// Send the length, then the command
uint32_t len = command.length();
write_all(fd, (char*)&len, 4);
write_all(fd, command.c_str(), len);
// Read the reply length, then the reply
uint32_t reply_len = 0;
if (read_full(fd, (char*)&reply_len, 4)) {
char buffer[4096] = {0};
read_full(fd, buffer, reply_len);
std::cout << buffer;
}
close(fd);
return 0;
}