Skip to content

Commit a2976bc

Browse files
committed
Added CLI menu for client. Now the client can send a message consisting of several words.
1 parent 3729aee commit a2976bc

File tree

3 files changed

+114
-55
lines changed

3 files changed

+114
-55
lines changed

client.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
#include "client.h"
22

3-
void client::ConnectToServer(char* host, char* port)
3+
void client::ConnectToServer(std::string& host, std::string& port)
44
{
5-
const unsigned int port_number = std::atoi(port);
6-
std::cout << "CONNECT TO " << host << ":" << port_number << std::endl;
7-
if (port_number > 0 && port_number < 65536)
8-
{
9-
ep = boost::asio::connect(socket_, resolver.resolve(host, port));
10-
std::cout << "SUCCESSFULLY CONNECTED" << std::endl;
11-
}
12-
else {
13-
std::cout << "UNABLE TO CONNECT" << std::endl;
14-
throw std::runtime_error("Error: Invalid Port. Port must be greter than 0 and less than 65536\n");
15-
}
5+
std::cout << "\tCONNECTING TO " << host << ":" << port << std::endl;
6+
ep = boost::asio::connect(socket_, resolver.resolve(host, port));
7+
std::cout << "\tSUCCESSFULLY CONNECTED" << std::endl;
168
}
179

18-
void client::Send()
10+
void client::Send(std::vector<std::string>& message)
1911
{
20-
std::cout << "CLIENT> SEND MSG: ";
21-
std::cin.getline(request, max_length);
22-
12+
char* buf = request;
13+
for (size_t i = 0; i < message.size(); ++i)
14+
{
15+
for (char c = 0; c < std::strlen(message[i].c_str()); c++)
16+
*buf++ = message[i][c];
17+
*buf++ = ' ';
18+
}
19+
*buf++ = '\0';
20+
2321
request_length = std::strlen(request);
2422
boost::asio::write(socket_, boost::asio::buffer(request, request_length));
25-
26-
std::cout << "CLIENT> MSG SEND TO " << ep.address() << ":" << ep.port() << "\tDATA: " << request << std::endl;
23+
24+
std::cout << "\tCLIENT MSG SEND TO " << ep.address() << ":" << ep.port() << "\tDATA: " << request << std::endl;
2725
}
2826

2927
void client::GetReply()
@@ -35,7 +33,8 @@ void client::GetReply()
3533
boost::asio::buffer(reply, request_length)
3634
);
3735

38-
std::cout << "CLIENT> REPLY: ";
36+
memset(request, 0, sizeof request);
37+
std::cout << "\tSERVER REPLY FROM " << ep.address() << ":" << ep.port() << "\tDATA: ";
3938
std::cout.write(reply, reply_length);
4039
std::cout << std::endl;
4140
}

client.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class client
1010
{
1111
public:
1212
client() :
13-
socket_(io_context),
14-
resolver(io_context),
13+
socket_(io_service),
14+
resolver(io_service),
1515
request_length(0)
1616
{
1717
}
1818

19-
void ConnectToServer(char* host, char* port);
20-
void Send();
19+
void ConnectToServer(std::string& host, std::string& port);
20+
void Send(std::vector<std::string>& message);
2121
void GetReply();
2222

2323
~client() noexcept
@@ -36,14 +36,14 @@ class client
3636

3737
private:
3838
enum { max_length = 1024 };
39-
boost::asio::io_context io_context;
40-
39+
boost::asio::io_service io_service;
40+
4141
tcp::socket socket_;
4242
tcp::resolver resolver;
4343
tcp::endpoint ep;
4444

4545
char request[max_length];
46-
size_t request_length;
46+
size_t request_length;
4747
};
4848

4949

main_client.cpp

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,95 @@
11
#include "client.h"
22

3-
int main(int argc, char* argv[])
3+
#include <cli/cli.h>
4+
#include <cli/clifilesession.h>
5+
6+
#include <boost/algorithm/string.hpp> // split
7+
#include <boost/lexical_cast.hpp> // lexical_cast
8+
9+
using namespace cli;
10+
11+
int main(void)
412
{
5-
try
6-
{
7-
if (argc != 3)
13+
client client;
14+
auto rootMenu = std::make_unique< Menu >("CLIENT");
15+
16+
rootMenu->Insert(
17+
"connect",
18+
[&](std::ostream& out, std::string host_port)
19+
{
20+
if (host_port != "")
21+
{
22+
try
23+
{
24+
std::vector<std::string> result_vect;
25+
boost::split(result_vect, host_port, boost::is_any_of(":"));
26+
27+
if (result_vect.size() == 2)
28+
{
29+
int port = boost::lexical_cast<int>(result_vect[1].c_str());
30+
if (port > 0 && port < 65536)
31+
{
32+
client.ConnectToServer(
33+
result_vect[0], // host
34+
result_vect[1] // port
35+
);
36+
//flag connected or not
37+
}
38+
else
39+
{
40+
std::cout << "\tUNABLE TO CONNECT" << std::endl;
41+
throw std::runtime_error("\tError: Invalid Port. Port must be greter than 0 and less than 65536\n");
42+
}
43+
}
44+
else
45+
{
46+
out << "\tConnect string wrong! Use: \"connect <string>\"" << std::endl;
47+
}
48+
}
49+
catch (const std::exception& ex)
50+
{
51+
out << "CLIENT> Connect exception: " << ex.what() << "\n";
52+
}
53+
}
54+
else
55+
{
56+
out << "\tConnect string must be not empty! Use: \"connect <string>\"" << std::endl;
57+
}
58+
},
59+
"Connect to Server");
60+
61+
rootMenu->Insert(
62+
"send",
63+
[&client](std::ostream& out, std::vector<std::string> msg)
864
{
9-
std::cerr << "CLIENT> Usage: ./client <host> <port>\n";
10-
return 1;
11-
}
12-
13-
/*HELP*/
14-
15-
/*CONNECT TO SERVER*/
16-
client client;
17-
client.ConnectToServer(
18-
argv[1], // host
19-
argv[2] // port
20-
);
21-
22-
/*SEND*/
23-
client.Send();
24-
client.GetReply();
25-
26-
/*EXIT*/
27-
//return 0;
28-
}
29-
catch (const std::exception& e)
30-
{
31-
std::cerr << "CLIENT> Exception: " << e.what() << "\n";
32-
}
65+
if (msg[0] != "")
66+
{
67+
try
68+
{
69+
//flag check
70+
client.Send(msg);
71+
client.GetReply();
72+
}
73+
catch (const std::exception& ex)
74+
{
75+
out << "CLIENT> Send exception: " << ex.what() << "\n";
76+
}
77+
}
78+
else
79+
{
80+
out << "\tMessage string must be not empty! Use: \"send <string>\"" << std::endl;
81+
}
82+
},
83+
"Send message");
84+
85+
Cli cli(std::move(rootMenu));
86+
cli.ExitAction([](auto& out)
87+
{
88+
out << "\tEXIT. Goodbye!\n";
89+
});
90+
91+
CliFileSession input(cli);
92+
input.Start();
3393

3494
return 0;
35-
}
95+
}

0 commit comments

Comments
 (0)