-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
78 lines (62 loc) · 2 KB
/
Copy pathclient.cpp
File metadata and controls
78 lines (62 loc) · 2 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
70
71
72
73
74
75
76
77
#include <iostream>
#include <algorithm>
#include <cstring>
#include <memory>
#include "sockets/sockets.h"
int main(int argc, char **argv)
{
char const localhos_ip[] = "127.0.0.1";
uint16_t const port = 6061;
int const BUF_SIZE = 66561; //65kb + 1b
char const syntax_mess[] = "syntax:\nclient PROTOCOL [SERVER IP]\nPROTOCOL - may be UDP or TCP\n";
if (argc < 2) {
std::cout << syntax_mess;
return 0;
}
char const *ip = localhos_ip;
if (argc > 2) {
ip = argv[2];
}
abstract_socket *tmp = nullptr;
if (!std::strcmp(argv[1], "UDP")) {
tmp = new socket_UDP();
} else if (!std::strcmp(argv[1], "TCP")) {
tmp = new socket_TCP();
} else {
std::cout << syntax_mess;
return 0;
}
std::unique_ptr<abstract_socket> sock(tmp);
if (!sock->is_valid()) {
std::cerr << "Creating socket error.\nReason: " << sock->get_er_message() << std::endl;
return 1;
}
if (!sock->connect(ip, port)) {
std::cerr << "Connection failed.\nReason: " << sock->get_er_message() << std::endl;
return 1;
}
for (;;) {
std::string s;
std::getline(std::cin, s);
if (!s.size()) {
continue;
}
int byte_count = sock->send(s.c_str(), s.size());
if (byte_count < 0) {
std::cerr << "Sending failed.\nReason: " << sock->get_er_message() << std::endl;
}
std::cout << "Sent " << byte_count << " bytes." << std::endl;
char buf[BUF_SIZE];
byte_count = sock->receive(buf, BUF_SIZE);
if (!byte_count) {
std::cout << "Connection is lost." << std::endl;
return 0;
}
if (byte_count < 0) {
std::cerr << "Receiving failed.\nReason: " << sock->get_er_message() << std::endl;
}
buf[std::min(byte_count, BUF_SIZE - 1)] = '\0';
std::cout << "Received " << byte_count << " bytes.\nMessage: " << buf << std::endl;
}
return 0;
}