• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 6,  comments - 61,  trackbacks - 0

            Daytime.4 - A synchronous UDP daytime client

            一個同步的UDP daytime客戶端

            This tutorial program shows how to use asio to implement a client application with UDP.

            本例示范如何使用Asio實現一個UDP客戶端程序。

            
            
            #include <iostream>
            #include 
            <boost/array.hpp>
            #include 
            <boost/asio.hpp>

            using boost::asio::ip::udp;

            The start of the application is essentially the same as for the TCP daytime client.

             應用程序的開始部分同TCP daytime客戶端在本質上是相同的。

            
            
            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;

            We use an boost::asio::ip::udp::resolver object to find the correct remote endpoint to use based on the host and service names. The query is restricted to return only IPv4 endpoints by the boost::asio::ip::udp::v4() argument.

            我們使用boost::asio::ip::udp::resolver對象來找到正確的基于主機和服務器名稱的遠程終端。使用boost::asio::ip::udp::v4()參數來限制query 僅僅返回IPv4的節點。

            
            
                udp::resolver resolver(io_service);
                udp::resolver::query query(udp::v4(), argv[
            1], "daytime");

            The boost::asio::ip::udp::resolver::resolve() function is guaranteed to return at least one endpoint in the list if it does not fail. This means it is safe to dereference the return value directly.

            如果函數沒有失敗,boost::asio::ip::udp::resolver::resolve() 保證至少返回列表中一個節點。這意味著直接解引用(提領)返回值是安全的。

            
            
                udp::endpoint receiver_endpoint = *resolver.resolve(query);

            Since UDP is datagram-oriented, we will not be using a stream socket. Create an boost::asio::ip::udp::socket and initiate contact with the remote endpoint.

            由于UDP(UDP不就是用戶數據報協議嘛)是基于數據報的,我們并不需要使用一個基于流的socket。創建一個boost::asio::ip::udp::socket后即可啟動同遠程終端的連接。
            
            
                udp::socket socket(io_service);
                socket.open(udp::v4());

                boost::array
            <char1> send_buf  = { 0 };
                socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);

            Now we need to be ready to accept whatever the server sends back to us. The endpoint on our side that receives the server's response will be initialised by boost::asio::ip::udp::socket::receive_from().

            現在,我們需要接收服務器發給我們的任何信息。接收服務器響應的客戶端節點boost::asio::ip::udp::socket::receive_from()啟動。
            
            
                boost::array<char128> recv_buf;
                udp::endpoint sender_endpoint;
                size_t len 
            = socket.receive_from(
                    boost::asio::buffer(recv_buf), sender_endpoint);

                std::cout.write(recv_buf.data(), len);
              }

            Finally, handle any exceptions that may have been thrown.

            最后,處理可能拋出的任何異常。

            
            
              catch (std::exception& e)
              {
                std::cerr 
            << e.what() << std::endl;
              }

              
            return 0;
            }

            See the full source listing

            全部源碼:

            //
            // client.cpp
            // ~~~~~~~~~~
            //
            // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
            //
            // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
            //

            #include 
            <iostream>
            #include 
            <boost/array.hpp>
            #include 
            <boost/asio.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 receiver_endpoint 
            = *resolver.resolve(query);

                udp::socket socket(io_service);
                socket.open(udp::v4());

                boost::array
            <char1> send_buf  = { 0 };
                socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);

                boost::array
            <char128> recv_buf;
                udp::endpoint sender_endpoint;
                size_t len 
            = socket.receive_from(
                    boost::asio::buffer(recv_buf), sender_endpoint);

                std::cout.write(recv_buf.data(), len);
              }
              
            catch (std::exception& e)
              {
                std::cerr 
            << e.what() << std::endl;
              }

              
            return 0;
            }

             

            Daytime.5 - A synchronous UDP daytime server

            一個同步的UDP daytime服務器

            This tutorial program shows how to use asio to implement a server application with UDP.

            本例示范如何使用Asio實現一個UDP服務器應用程序。

            
            
            int main()
            {
              
            try
              {
                boost::asio::io_service io_service;

            Create an boost::asio::ip::udp::socket object to receive requests on UDP port 13.

            創建一個boost::asio::ip::udp::socket對象用來接收來自UDP端口13的請求。

            
            
             udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));

            Wait for a client to initiate contact with us. The remote_endpoint object will be populated by boost::asio::ip::udp::socket::receive_from().

            服務器一直等待直到客戶端發起同它的連接。remote_endpoint 對象將被boost::asio::ip::udp::socket::receive_from()初始化。

            
            
                for (;;)
                {
                  boost::array
            <char1> recv_buf;
                  udp::endpoint remote_endpoint;
                  boost::system::error_code error;
                  socket.receive_from(boost::asio::buffer(recv_buf),
                      remote_endpoint, 
            0, error);

                  
            if (error && error != boost::asio::error::message_size)
                    
            throw boost::system::system_error(error);

            Determine what we are going to send back to the client.

             確定服務器將要傳回給客戶端的信息。

            
            
                  std::string message = make_daytime_string();

            Send the response to the remote_endpoint.

            給信息發送給遠程終端。

            
            
                  boost::system::error_code ignored_error;
                  socket.send_to(boost::asio::buffer(message),
                      remote_endpoint, 
            0, ignored_error);
                }
              }

            Finally, handle any exceptions.

            最后,處理任何異常。

            
            
              catch (std::exception& e)
              {
                std::cerr 
            << e.what() << std::endl;
              }

              
            return 0;
            }

            See the full source listing

            //
            // server.cpp
            // ~~~~~~~~~~
            //
            // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
            //
            // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
            //

            #include 
            <ctime>
            #include 
            <iostream>
            #include 
            <string>
            #include 
            <boost/array.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);
            }

            int main()
            {
              
            try
              {
                boost::asio::io_service io_service;

                udp::socket socket(io_service, udp::endpoint(udp::v4(), 
            13));

                
            for (;;)
                {
                  boost::array
            <char1> recv_buf;
                  udp::endpoint remote_endpoint;
                  boost::system::error_code error;
                  socket.receive_from(boost::asio::buffer(recv_buf),
                      remote_endpoint, 
            0, error);

                  
            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;
            }

            Daytime.5 - A synchronous UDP daytime server

            一個同步的UDP daytime服務器

            This tutorial program shows how to use asio to implement a server application with UDP.

            本例示范如何使用Asio實現一個UDP服務器應用程序。

            
            
            int main()
            {
              
            try
              {
                boost::asio::io_service io_service;

            Create an boost::asio::ip::udp::socket object to receive requests on UDP port 13.

            創建一個boost::asio::ip::udp::socket對象用來接收來自UDP端口13的請求。

            
            
             udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));

            Wait for a client to initiate contact with us. The remote_endpoint object will be populated by boost::asio::ip::udp::socket::receive_from().

            服務器一直等待直到客戶端發起同它的連接。remote_endpoint 對象將被boost::asio::ip::udp::socket::receive_from()初始化。

            
            
                for (;;)
                {
                  boost::array
            <char1> recv_buf;
                  udp::endpoint remote_endpoint;
                  boost::system::error_code error;
                  socket.receive_from(boost::asio::buffer(recv_buf),
                      remote_endpoint, 
            0, error);

                  
            if (error && error != boost::asio::error::message_size)
                    
            throw boost::system::system_error(error);

            Determine what we are going to send back to the client.

             確定服務器將要傳回給客戶端的信息。

            
            
                  std::string message = make_daytime_string();

            Send the response to the remote_endpoint.

            給信息發送給遠程終端。

            
            
                  boost::system::error_code ignored_error;
                  socket.send_to(boost::asio::buffer(message),
                      remote_endpoint, 
            0, ignored_error);
                }
              }

            Finally, handle any exceptions.

            最后,處理任何異常。

            
            
              catch (std::exception& e)
              {
                std::cerr 
            << e.what() << std::endl;
              }

              
            return 0;
            }

            See the full source listing

            //
            // server.cpp
            // ~~~~~~~~~~
            //
            // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
            //
            // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
            //

            #include 
            <ctime>
            #include 
            <iostream>
            #include 
            <string>
            #include 
            <boost/array.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);
            }

            int main()
            {
              
            try
              {
                boost::asio::io_service io_service;

                udp::socket socket(io_service, udp::endpoint(udp::v4(), 
            13));

                
            for (;;)
                {
                  boost::array
            <char1> recv_buf;
                  udp::endpoint remote_endpoint;
                  boost::system::error_code error;
                  socket.receive_from(boost::asio::buffer(recv_buf),
                      remote_endpoint, 
            0, error);

                  
            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;
            }
            posted on 2008-04-21 09:10 王曉軒 閱讀(3472) 評論(2)  編輯 收藏 引用 所屬分類: C\C++
            久久精品国产亚洲精品2020 | 久久免费高清视频| 久久久久久久尹人综合网亚洲| 人人狠狠综合久久亚洲88| 亚洲欧美久久久久9999| 国产亚洲精久久久久久无码| 超级碰久久免费公开视频| 久久无码专区国产精品发布 | 精品国产91久久久久久久a| 久久人人爽人人人人爽AV| 久久综合九色综合久99| 久久人人爽人人爽人人爽| 精品一区二区久久久久久久网站| 一本一道久久a久久精品综合| 久久精品人人做人人妻人人玩| 日韩欧美亚洲国产精品字幕久久久| 午夜久久久久久禁播电影| 久久夜色精品国产| 热久久国产精品| av国内精品久久久久影院| 亚洲欧美国产精品专区久久| 国产99久久久国产精品~~牛| 久久精品无码午夜福利理论片| 日本欧美国产精品第一页久久| 久久久九九有精品国产| 久久久久成人精品无码中文字幕| 四虎国产精品成人免费久久 | 久久99国产精品久久久| 久久精品亚洲AV久久久无码| 成人a毛片久久免费播放| 国产一区二区三区久久| 久久久久久国产精品免费无码| 久久天天躁狠狠躁夜夜躁2014| 亚洲精品无码专区久久同性男| 久久激情亚洲精品无码?V| 伊人久久免费视频| a级毛片无码兔费真人久久| 66精品综合久久久久久久| 久久综合九色综合97_久久久| 国产精品美女久久久| 精品国产91久久久久久久|