• <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这里只有精品国产| 欧美色综合久久久久久| 欧美日韩中文字幕久久久不卡 | 久久人做人爽一区二区三区| 久久综合久久鬼色| 精品国产乱码久久久久软件| 伊色综合久久之综合久久| 亚洲精品乱码久久久久久不卡| 久久亚洲2019中文字幕| 中文字幕无码久久久| 久久精品国产亚洲αv忘忧草| 久久精品国产99久久久古代| 伊人久久精品无码av一区| 性做久久久久久久| 久久久久免费精品国产| 久久成人精品| 99久久综合国产精品免费| 色偷偷久久一区二区三区| 久久精品免费观看| 久久精品国产亚洲一区二区三区| 免费一级欧美大片久久网| 久久精品国产精品亚洲精品| 亚洲中文字幕久久精品无码APP | 久久婷婷五月综合色99啪ak| 亚洲成av人片不卡无码久久| 亚洲人AV永久一区二区三区久久| 欧美大香线蕉线伊人久久| 国产日韩欧美久久| 超级97碰碰碰碰久久久久最新| 精品久久久久中文字幕日本| 99久久综合狠狠综合久久| 国产毛片欧美毛片久久久| 国产精品狼人久久久久影院| 亚洲中文字幕无码一久久区| 91精品国产91热久久久久福利|