Server
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 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#include <thread> #include <iostream> #include <string> #include <boost/asio.hpp> #include <boost/array.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost::asio; using namespace ip; #define IPADDRESS "192.168.1.206" #define UDP_PORT 1251 bool SetProcessPrivileges(const wchar_t* privileges) { HANDLE processToken; TOKEN_PRIVILEGES tp; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &processToken)) return false; if (!LookupPrivilegeValue(NULL, privileges, &tp.Privileges[0].Luid)) return false; tp.PrivilegeCount = 1; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(processToken, false, &tp, 0, 0, 0); if (GetLastError() != ERROR_SUCCESS) return false; return true; } struct Ruberoid { boost::asio::io_service io_service; udp::socket socket{ io_service }; boost::array<char, 1024> recv_buffer; udp::endpoint remote_endpoint; void handle_receive(const boost::system::error_code& error, size_t bytes_transferred) { if (error) { std::cout << "Receive failed: " << error.message() << "\n"; return; } std::cout << "Received: '" << std::string(recv_buffer.begin(), recv_buffer.begin() + bytes_transferred) << "' (" << error.message() << ")\n"; if (std::string(recv_buffer.begin(), recv_buffer.begin() + bytes_transferred) == "tushisvet") { SetProcessPrivileges(SE_SHUTDOWN_NAME); ExitWindowsEx(EWX_POWEROFF | EWX_FORCEIFHUNG, 0); wait(); } } void wait() { socket.async_receive_from(boost::asio::buffer(recv_buffer), remote_endpoint, boost::bind(&Ruberoid::handle_receive, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void Receiver() { socket.open(udp::v4()); socket.bind(udp::endpoint(address::from_string(IPADDRESS), UDP_PORT)); wait(); std::cout << "Receiving\n"; io_service.run(); std::cout << "Receiver exit\n"; } }; int main(int argc, char* argv[]){ Ruberoid ruberoid; std::thread r([&] { ruberoid.Receiver(); }); std::string input = argc > 1 ? argv[1] : "hello world"; std::cout << "Input is '" << input.c_str() << "'\nSending it to Sender Function...\n"; r.join(); // std::getchar(); cin.get(); } |
Client
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 |
#include <iostream> #include <string> #include <boost\asio.hpp> #include <boost\array.hpp> using namespace std; using namespace boost::asio; using namespace ip; #define IPADDRESS "192.168.1.206" #define UDP_PORT 1251 int main(int argc, char *argv[]) try{ std::string input = argc>1? argv[1] : "hello world"; std::cout << "Input is '" << input.c_str() << "'\nSending it to Sender Function...\n"; boost::asio::io_service io_service; udp::socket socket(io_service); udp::endpoint remote_endpoint = udp::endpoint(address::from_string(IPADDRESS), UDP_PORT); socket.open(udp::v4()); boost::system::error_code err; auto sent = socket.send_to(boost::asio::buffer("tushisvet"), remote_endpoint, 0, err); socket.close(); std::cout << "Sent Payload --- " << sent << "\n"; //std::getchar(); cin.get(); } catch (const std::exception& err) { std::cerr << err.what(); } |