| 110 | //---------------------------------------------------------------------- |
| 111 | |
| 112 | int main(int argc, char* argv[]) |
| 113 | { |
| 114 | try |
| 115 | { |
| 116 | using namespace std; // For atoi. |
| 117 | |
| 118 | if (argc != 3) |
| 119 | { |
| 120 | std::cerr << "Usage: blocking_udp_client <listen_addr> <listen_port>\n"; |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | udp::endpoint listen_endpoint( |
| 125 | boost::asio::ip::make_address(argv[1]), |
| 126 | std::atoi(argv[2])); |
| 127 | |
| 128 | client c(listen_endpoint); |
| 129 | |
| 130 | for (;;) |
| 131 | { |
| 132 | char data[1024]; |
| 133 | boost::system::error_code error; |
| 134 | std::size_t n = c.receive(boost::asio::buffer(data), |
| 135 | std::chrono::seconds(10), error); |
| 136 | |
| 137 | if (error) |
| 138 | { |
| 139 | std::cout << "Receive error: " << error.message() << "\n"; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | std::cout << "Received: "; |
| 144 | std::cout.write(data, n); |
| 145 | std::cout << "\n"; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | catch (std::exception& e) |
| 150 | { |
| 151 | std::cerr << "Exception: " << e.what() << "\n"; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |