This repository was archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestClient.cpp
More file actions
71 lines (65 loc) · 1.94 KB
/
testClient.cpp
File metadata and controls
71 lines (65 loc) · 1.94 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
#include <boost/beast.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <iostream>
namespace net = boost::asio;
namespace beast = boost::beast;
using namespace boost::beast;
using namespace boost::beast::websocket;
int main(int argc, char const *argv[]) {
try
{
net::io_context ioc; //i_o context req for all I/O
net::ssl::context ctx(net::ssl::context::tlsv12); //context for SSL security
//Set up for SSL
ctx.set_default_verify_paths();
ctx.use_certificate_file("CNProjectNewCert.pem", net::ssl::context::pem); //Generated Cert
ctx.use_rsa_private_key_file("CNProject.pem",net::ssl::context::pem); //Generated Key
//Set up network connection
std::string host = "71.213.21.10"; //IP address for client to connect to
stream<net::ssl::stream<net::ip::tcp::socket>> ws(ioc, ctx);
net::ip::tcp::resolver resolver(ioc);
auto const resolved = resolver.resolve(host, "80"); //connect through port 80
// Connect the socket to the IP address returned from performing a name lookup
net::connect(ws.lowest_layer(), resolved.begin(), resolved.end());
//Get user name to display with messages
std::string name;
std::cout << "State your name: ";
std::cin >> name;
bool flag = false;
ws.next_layer().handshake(net::ssl::stream_base::client);
ws.handshake(host, "/");
std::cout << "Connected." << '\n';
std::string msg;
ws.text(true);
while(msg != "!quit~")
{
if(flag)
{
std::cout << "[" << name << "]: ";
}
std::getline(std::cin,msg);
if(flag)
{
ws.write(net::buffer("["+name+"]: "+msg));
}
flag=true;
}
ws.close(close_code::normal);
}
catch(error_code ec)
{
if(ec == websocket::error::closed)
{
return 0;
}
else
{
int nothing;
std::cout << ec << '\n';
std::cin >> nothing;
}
}
return 0;
}