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 pathtestServer.cpp
More file actions
72 lines (66 loc) · 2.15 KB
/
testServer.cpp
File metadata and controls
72 lines (66 loc) · 2.15 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
#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 required for all I/O
//set up SSL
net::ssl::context ctx(net::ssl::context::tlsv12);
ctx.use_certificate_file("CNProjectNewCert.pem", net::ssl::context::pem);
ctx.use_rsa_private_key_file("CNProject.pem",net::ssl::context::pem);
//set up server with IP connection
net::ip::tcp::acceptor acceptor(ioc);
net::ip::tcp::endpoint endpoint(net::ip::tcp::v4(), 80); //get IP address of device for server
acceptor.open(endpoint.protocol());
acceptor.bind(endpoint);
std::cout << "Endpoint Bound." << '\n';
//Check for messages loop
while(true)
{
acceptor.listen();
std::cout << "Listening..." << '\n';
// The socket returned by accept() will be forwarded to the tcp_stream,
// which uses it to perform a move-construction from the net::ip::tcp::socket.
stream<net::ssl::stream<net::ip::tcp::socket>> ws(acceptor.accept(),ctx);
// Perform the websocket handshake in the server role.
// The stream must already be connected to the peer.
ws.next_layer().handshake(net::ssl::stream_base::server);
ws.accept();
std::cout << "Handshake complete." << '\n';
multi_buffer buffer;
while(buffers_to_string(buffer.data()).find("!quit~") == std::string::npos)
{
std::string msg;
std::stringstream msg_buffer;
multi_buffer reset;
buffer=reset;
ws.read(buffer);
ws.text(ws.got_text());
msg_buffer << buffers_to_string(buffer.data());
msg = msg_buffer.str();
std::cout << msg << "\n";
}
}
}
catch(error_code ec)
{
if(ec == net::error::operation_aborted||ec==websocket::error::closed)
{
return 0;
}
else
{
int nothing;
std::cout << ec << '\n';
std::cin >> nothing;
}
}
return 0;
}