|
@@ -4,107 +4,27 @@
|
|
|
#include <iostream>
|
|
|
#include <thread>
|
|
|
|
|
|
+#include "../include/Server.h"
|
|
|
#include "../include/Sniffer.h"
|
|
|
|
|
|
-using namespace boost::asio;
|
|
|
-using ip::tcp;
|
|
|
-using std::cout;
|
|
|
-using std::endl;
|
|
|
-
|
|
|
-class con_handler : public boost::enable_shared_from_this<con_handler> {
|
|
|
-private:
|
|
|
- tcp::socket sock;
|
|
|
- std::string message = "Hello From Server!";
|
|
|
- enum { max_length = 1024 };
|
|
|
- char data[max_length];
|
|
|
-
|
|
|
-public:
|
|
|
- typedef boost::shared_ptr<con_handler> pointer;
|
|
|
- con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service)
|
|
|
- : sock(io_service) {}
|
|
|
-
|
|
|
- static pointer
|
|
|
- create(basic_socket_acceptor<ip::tcp>::executor_type &io_service) {
|
|
|
- return pointer(new con_handler(io_service));
|
|
|
- }
|
|
|
-
|
|
|
- tcp::socket &socket() { return sock; }
|
|
|
-
|
|
|
- void start() {
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
-
|
|
|
- sock.async_write_some(buffer(message, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
- }
|
|
|
-
|
|
|
- void handle_read(const boost::system::error_code &err,
|
|
|
- size_t bytes_transferred) {
|
|
|
- if (!err) {
|
|
|
- cout << data << endl;
|
|
|
- } else {
|
|
|
- std::cerr << "error: " << err.message() << std::endl;
|
|
|
- sock.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- void handle_write(const boost::system::error_code &err,
|
|
|
- size_t bytes_transferred) {
|
|
|
- if (!err) {
|
|
|
- cout << "Hello World!" << endl;
|
|
|
- } else {
|
|
|
- std::cerr << "error: " << err.message() << endl;
|
|
|
- sock.close();
|
|
|
- }
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-class Server {
|
|
|
-private:
|
|
|
- tcp::acceptor acceptor_;
|
|
|
- void start_accept() {
|
|
|
- auto executor = acceptor_.get_executor();
|
|
|
- con_handler::pointer connection = con_handler::create(executor);
|
|
|
- acceptor_.async_accept(connection->socket(),
|
|
|
- boost::bind(&Server::handle_accept, this, connection,
|
|
|
- placeholders::error));
|
|
|
- }
|
|
|
-
|
|
|
-public:
|
|
|
- Server(io_service &io_service)
|
|
|
- : acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234)) {
|
|
|
- start_accept();
|
|
|
- }
|
|
|
- void handle_accept(con_handler::pointer connection,
|
|
|
- const boost::system::error_code &err) {
|
|
|
- if (!err) {
|
|
|
- connection->start();
|
|
|
- }
|
|
|
- start_accept();
|
|
|
- }
|
|
|
-};
|
|
|
+using namespace std;
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
if (argc < 2) {
|
|
|
- std::cout << "Usage: " << argv[0] << " <interface>" << std::endl
|
|
|
- << std::endl;
|
|
|
+ cout << "Usage: " << argv[0] << " <interface>" << endl << endl;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- const std::string interface = argv[1];
|
|
|
+ const string interface = argv[1];
|
|
|
Sniffer sniffer(interface);
|
|
|
- std::thread snifferThread(&Sniffer::startSniffing, sniffer);
|
|
|
+ thread snifferThread(&Sniffer::startSniffing, sniffer);
|
|
|
|
|
|
try {
|
|
|
io_service io_service;
|
|
|
Server server(io_service);
|
|
|
io_service.run();
|
|
|
- } catch (std::exception &e) {
|
|
|
- std::cerr << e.what() << endl;
|
|
|
+ } catch (exception &e) {
|
|
|
+ cerr << e.what() << endl;
|
|
|
}
|
|
|
|
|
|
snifferThread.join();
|