-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasioChat.cpp
134 lines (92 loc) · 2.65 KB
/
asioChat.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <asio.hpp>
#define eLOG(x) std::cout << "\033[1;31m[ERROR] \033[0m" << x << std::endl;
#define wLOG(x) std::cout << "\033[0;33m[WARNING] \033[0m" << x << std::endl;
#define iLOG(x) std::cout << "\033[0;32m[INFO] \033[0m" << x << std::endl;
#define LOG(x) std::cout << x << std::endl;
using asio::ip::tcp;
void hostChat(tcp::socket& sock, std::string& uname){
bool running{ true };
asio::streambuf buf;
std::string reply;
std::string msg;
while (running) {
asio::read_until(sock, buf, '\n');
msg = asio::buffer_cast<const char*>(buf.data());
std::cout << msg << std::endl;
buf.consume(buf.size());
std::getline(std::cin, reply);
reply = uname + " : [MSG] " + reply;
asio::write(sock, asio::buffer(reply + '\n'));
}
sock.close();
}
void accept(asio::io_context& ctx, tcp::acceptor& acceptor, std::string& uname) {
tcp::socket sock(ctx);
acceptor.accept(sock);
hostChat(sock, uname);
}
void host(asio::io_context& ctx) {
bool running;
short port;
std::string uname;
system("cls");
iLOG("Choose PORT");
std::cin >> port;
iLOG("Choose username");
std::cin >> uname;
system("cls");
iLOG(port);
tcp::endpoint endpoint(tcp::v4(), port);
tcp::acceptor acceptor(ctx, endpoint);
accept(ctx, acceptor, uname);
}
void client(asio::io_context& ctx) {
system("cls");
std::string ip, uname;
short port;
bool running{ true };
asio::streambuf buf;
iLOG("Choose a username");
std::cin >> uname;
iLOG("Connect to an IP (127.0.0.1)");
std::cin >> ip;
iLOG("Connect to a port (8080)");
std::cin >> port;
tcp::resolver resolver(ctx);
tcp::resolver::results_type endpoint = resolver.resolve(ip, std::to_string(port));
tcp::socket sock(ctx);
asio::connect(sock, endpoint);
system("cls");
std::string reply;
std::string msg;
while (running) {
std::getline(std::cin, reply);
reply = uname + " : [MSG] " + reply;
asio::write(sock, asio::buffer(reply + '\n'));
asio::read_until(sock, buf, '\n');
msg = asio::buffer_cast<const char*>(buf.data());
std::cout << msg << std::endl;
buf.consume(buf.size());
}
sock.close();
}
int main() {
system("cls");
wLOG("This application is not async");
wLOG("This means that you can send a message and then you have to wait for the other user to send a message\n");
iLOG("APPLICATION STYLE: (c)lient or (h)ost");
char choice;
asio::io_context ctx;
std::cin >> choice;
if (choice != 'h' && choice != 'c') {
eLOG("INVALID CHOICE");
}
else if (choice == 'c') {
client(ctx);
}
else {
host(ctx);
}
return 0;
}