• <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++
            天天久久狠狠色综合| 午夜精品久久久久久中宇| 久久最近最新中文字幕大全 | 久久久久久久女国产乱让韩| 久久精品国产99国产精品亚洲| 久久亚洲欧美国产精品| 狠狠人妻久久久久久综合蜜桃| 久久综合九色欧美综合狠狠| 久久久久成人精品无码中文字幕| 久久r热这里有精品视频| 久久国产成人亚洲精品影院| 伊人久久大香线蕉综合影院首页| 久久伊人精品青青草原高清| 久久久久亚洲av成人网人人软件 | 久久精品国产99久久香蕉| 久久人人爽人人爽人人片av麻烦| 国产精品久久久久久福利69堂| 美女久久久久久| 久久综合综合久久97色| 无码人妻久久一区二区三区免费丨| 91精品久久久久久无码| 欧美一区二区三区久久综| 久久黄色视频| 99久久99这里只有免费的精品| 成人午夜精品无码区久久| 久久强奷乱码老熟女网站| 一本久久a久久精品综合夜夜| 亚洲精品无码久久久久| 合区精品久久久中文字幕一区| 欧美激情精品久久久久| 国产精品久久久福利| 久久久久久久亚洲Av无码| 久久久久99精品成人片试看| 狠狠色丁香久久婷婷综合| 久久久久se色偷偷亚洲精品av| 久久久av波多野一区二区| 久久国产精品成人免费 | 99久久香蕉国产线看观香| 久久青青草原精品影院| 国产成人精品免费久久久久| 99久久精品午夜一区二区|