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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include"endpoint.h" #include <boost/asio.hpp> #include <iostream> using namespace boost;
int client_end_point() { std::string raw_ip_address = "127.4.8.1"; unsigned short port_num = 3333;
boost::system::error_code ec; asio::ip::address ip_address = asio::ip::address::from_string(raw_ip_address, ec); if (ec.value() != 0) { std::cout << "Failed to parse the IP address. Error code = "<< ec.value() << ". Message: " << ec.message(); return ec.value(); }
asio::ip::tcp::endpoint ep(ip_address, port_num); return 0; }
int server_end_point() { unsigned short port_num = 3333; asio::ip::address ip_address = asio::ip::address_v6::any(); asio::ip::tcp::endpoint ep(ip_address, port_num); return 0; }
int create_tcp_socket() {
asio::io_context ioc; asio::ip::tcp protocol = asio::ip::tcp::v4(); asio::ip::tcp::socket sock(ioc); boost::system::error_code ec; sock.open(protocol, ec); if (ec.value() != 0) { std::cout << "Failed to parse the socket. Error code = " << ec.value() << ". Message: " << ec.message(); return ec.value(); } return 0; }
int create_acceptor_socket() { asio::io_context ioc;
asio::ip::tcp::acceptor acceptor(ioc, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 3333)); return 0; }
int bind_acceptor_socket() { unsigned short port_num = 3333; asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(),port_num); asio::io_context ios; asio::ip::tcp::acceptor acceptor(ios, ep.protocol()); boost::system::error_code ec; acceptor.bind(ep, ec); if (ec.value() != 0) { std::cout << "Failed to bind the socket. Error code = " << ec.value() << ". Message: " << ec.message(); return ec.value(); } return 0; }
int connnect_to_end() { std::string raw_ip_address = "127.0.0.1"; unsigned short port_num = 3333;
try { asio::ip::tcp::endpoint ep(asio::ip::address::from_string(raw_ip_address), port_num); asio::io_context ios; asio::ip::tcp::socket sock(ios, ep.protocol()); sock.connect(ep); } catch (system::system_error& e) { std::cout << "Error occured! Error code = " << e.code() << ". Message: " << e.what(); return e.code().value(); } return 0; }
int dns_connect_to_end() { std::string host = "samplehost"; std::string port_num = "3333"; asio::io_context ios;
asio::ip::tcp::resolver::query resolver_query(host, port_num, asio::ip::tcp::resolver::query::numeric_service); asio::ip::tcp::resolver resolver(ios); try { asio::ip::tcp::resolver::iterator it = resolver.resolve(resolver_query); asio::ip::tcp::socket sock(ios); asio::connect(sock,it); } catch (system::system_error& e) { std::cout << "Error occured! Error code = " << e.code() << ". Message: " << e.what(); return e.code().value(); } return 0; }
int accept_new_connection() { const int BACKLOG_SIZE = 30;
unsigned short port_num = 3333; asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(),port_num);
asio::io_context ios; try { asio::ip::tcp::acceptor acceptor(ios, ep.protocol()); acceptor.bind(ep); acceptor.listen(BACKLOG_SIZE); asio::ip::tcp::socket sock(ios); acceptor.accept(sock); } catch (system::system_error& e) { std::cout << "Error occured! Error code = " << e.code() << ". Message: " << e.what(); return e.code().value(); } return 0; }
|