File tree Expand file tree Collapse file tree 3 files changed +22
-19
lines changed
Expand file tree Collapse file tree 3 files changed +22
-19
lines changed Original file line number Diff line number Diff line change 11#include " client.h"
22
3- void client::ConnectToServer (std::string& host, std::string& port)
3+ void client::ConnectToServer (const std::string& host, const std::string& port)
44{
55 std::cout << " \t CONNECTING TO " << host << " :" << port << std::endl;
66 ep = boost::asio::connect (socket_, resolver.resolve (host, port));
7- std::cout << " \t SUCCESSFULLY CONNECTED" << std::endl;
7+ std::cout << " \t SUCCESSFULLY CONNECTED\n " << std::endl;
88}
99
10- void client::Send (std::vector<std::string>& message)
10+ void client::Send (const std::vector<std::string>& message)
1111{
1212 char * buf = request;
1313 for (size_t i = 0 ; i < message.size (); ++i)
1414 {
15- for (char c = 0 ; c < std::strlen (message[i].c_str ()); c++)
15+ for (size_t c = 0 ; c < std::strlen (message[i].c_str ()); c++)
1616 *buf++ = message[i][c];
1717 *buf++ = ' ' ;
1818 }
@@ -36,5 +36,5 @@ void client::GetReply()
3636 memset (request, 0 , sizeof request);
3737 std::cout << " \t SERVER REPLY FROM " << ep.address () << " :" << ep.port () << " \t DATA: " ;
3838 std::cout.write (reply, reply_length);
39- std::cout << std::endl;
39+ std::cout << std::endl << std::endl ;
4040}
Original file line number Diff line number Diff line change 11#pragma once
2- #include < cstdlib>
3- #include < cstring>
42#include < iostream>
53#include < boost/asio.hpp>
64
@@ -16,8 +14,8 @@ class client
1614 {
1715 }
1816
19- void ConnectToServer (std::string& host, std::string& port);
20- void Send (std::vector<std::string>& message);
17+ void ConnectToServer (const std::string& host, const std::string& port);
18+ void Send (const std::vector<std::string>& message);
2119 void GetReply ();
2220
2321 ~client () noexcept
@@ -42,7 +40,7 @@ class client
4240 tcp::resolver resolver;
4341 tcp::endpoint ep;
4442
45- char request[max_length];
43+ char request[max_length] = { 0 } ;
4644 size_t request_length;
4745};
4846
Original file line number Diff line number Diff line change @@ -11,13 +11,14 @@ using namespace cli;
1111int main (void )
1212{
1313 client client;
14+ bool connected = false ;
1415 auto rootMenu = std::make_unique< Menu >(" CLIENT" );
1516
1617 rootMenu->Insert (
1718 " connect" ,
18- [&](std::ostream& out, std::string host_port)
19+ [&](std::ostream& out, const std::string& host_port)
1920 {
20- if (host_port != " " )
21+ if (!host_port. empty () )
2122 {
2223 try
2324 {
@@ -33,11 +34,11 @@ int main(void)
3334 result_vect[0 ], // host
3435 result_vect[1 ] // port
3536 );
36- // flag connected or not
37+ connected = true ;
3738 }
3839 else
3940 {
40- std::cout << " \t UNABLE TO CONNECT" << std::endl;
41+ std::cout << " \t UNABLE TO CONNECT\n " << std::endl;
4142 throw std::runtime_error (" \t Error: Invalid Port. Port must be greter than 0 and less than 65536\n " );
4243 }
4344 }
@@ -60,15 +61,19 @@ int main(void)
6061
6162 rootMenu->Insert (
6263 " send" ,
63- [&client](std::ostream& out, std::vector<std::string> msg)
64+ [&client, &connected ](std::ostream& out, const std::vector<std::string>& msg)
6465 {
65- if (msg[ 0 ] != " " )
66+ if (msg. size () > 0 )
6667 {
6768 try
6869 {
69- // flag check
70- client.Send (msg);
71- client.GetReply ();
70+ if (connected) {
71+ client.Send (msg);
72+ client.GetReply ();
73+ }
74+ else {
75+ out << " \t You need to connect to the server! Use: \" connect <string>\" " << " \n " ;
76+ }
7277 }
7378 catch (const std::exception& ex)
7479 {
You can’t perform that action at this time.
0 commit comments