linux下使用boost庫編寫UDP客戶端/伺服器程式官方簡易教程 阿新 • • 發佈:2019-01-03 1.同步方式的UDP-daytime客戶端 原始碼:p.cpp #include <iostream> #include <boost/asio.hpp> #include <boost/array.hpp> using boost::asio::ip::udp; int main(int argc, char* argv[]) { try { if(argc != 2){ std::cerr << "Usage: client <host>" <<std::endl; return 1; } boost::asio::io_service io_service; udp::resolver resolver(io_service); udp::resolver::query query(udp::v4(), argv[1], "daytime"); udp::endpoint reciver_endpoint = *resolver.resolve(query); //得到對方的套接字 udp::socket socket(io_service); socket.open(udp::v4()); //開啟自己的UDP套接字 boost::array<char, 1> send_buf = {{0}}; socket.send_to(boost::asio::buffer(send_buf),reciver_endpoint); //向對方的套接字埠傳送報文 boost::array<char, 128> recv_buf; udp::endpoint sender_endpoint; size_t len = socket.receive_from(boost::asio::buffer(recv_buf), sender_endpoint); //接收報文,將來者套接字儲存在sender_endpoint中 std::cout.write(recv_buf.data(), len); //列印收到的內容 } catch(std::exception &e) { std::cerr << e.what() << std::endl; } return 0; } 編譯 :g++ p.cpp -lboost_system -lpthread -o p.out ============================================================================================== 2.同步方式的UDP-daytime伺服器 原始碼:q.cpp #include <iostream> #include <string> #include <ctime> #include <boost/asio.hpp> #include <boost/array.hpp> using boost::asio::ip::udp; std::string make_daytime_string() { using namespace std;//For time_t, time and ctime; time_t now = time(0); return ctime(&now); } int main() { try { boost::asio::io_service io_service; udp::socket socket(io_service, udp::endpoint(udp::v4(), 13)); //建立一個套接字在UDP埠13上監聽 for(; ; ) { boost::array<char, 1> recv_buf; udp::endpoint remote_endpoint; boost::system::error_code error; socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint, 0, error); //由接受到的資料報中得到時間請求者的地址,儲存在remote_endpoint中 if(error && error != boost::asio::error::message_size) throw boost::system::system_error(error); std::string message = make_daytime_string(); boost::system::error_code ignored_error; socket.send_to(boost::asio::buffer(message), remote_endpoint, 0, ignored_error); //向請求者傳送時間資訊 } } catch(std::exception &e) { std::cerr << e.what() << std::endl; } return 0; } 編譯 :g++ q.cpp -lboost_system -lpthread -o q.out ============================================================================================== 3.非同步方式的UDP-daytime伺服器 原始碼:r.cpp #include <ctime> #include <iostream> #include <string> #include <boost/array.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/asio.hpp> using boost::asio::ip::udp; std::string make_daytime_string() { using namespace std; // For time_t, time and ctime; time_t now = time(0); return ctime(&now); } class udp_server { public: udp_server(boost::asio::io_service& io_service) : socket_(io_service, udp::endpoint(udp::v4(), 13)) //初始化埠為13的UDP套接字 { start_receive(); } private: void start_receive() { socket_.async_receive_from( boost::asio::buffer(recv_buffer_), remote_endpoint_, boost::bind(&udp_server::handle_receive, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void handle_receive(const boost::system::error_code& error, std::size_t /*bytes_transferred*/) { if (!error || error == boost::asio::error::message_size) { boost::shared_ptr<std::string> message( new std::string(make_daytime_string())); socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_, boost::bind(&udp_server::handle_send, this, message, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); //傳送操作完成後執行handle_send控制代碼 start_receive(); } } void handle_send(boost::shared_ptr<std::string> /*message*/, const boost::system::error_code& /*error*/, std::size_t /*bytes_transferred*/) { } udp::socket socket_; udp::endpoint remote_endpoint_; boost::array<char, 1> recv_buffer_; }; int main() { try { boost::asio::io_service io_service; udp_server server(io_service); io_service.run(); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; } 編譯 :g++ r.cpp -lboost_system -lpthread -o r.out