[Boost] ASIO學習筆記:network basic– UDP Server
UDP Server跟TCP Server不同,它是屬於connectionless的技術,因此不需要acceptor去跟client建立連線的步驟。我們只需要利用socket去bind特定的port後,就可以接收到資料。 一個Sync socket的流程如下,圖片引用自此: 在ASIO中的Server端程式碼: #include <boost/asio.hpp> #include <boost/shared_ptr.hpp> #include <boost/bind.hpp> #include <iostream> #include <string> #include <boost/thread.hpp> using namespace std; void main(){ boost::system::error_code ec; boost::shared_ptr<boost::asio::io_service> io_service(new boost::asio::io_service); boost::shared_ptr<boost::asio::io_service::strand> strand(new boost::asio::io_service::strand(*io_service)); // boost::shared_ptr< boost::asio::io_service::work > work(new boost::asio::io_service::work( *io_service )); try{ … Continued