• <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實現(xiàn)一個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客戶端在本質(zhì)上是相同的。

            
            
            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()參數(shù)來限制query 僅僅返回IPv4的節(jié)點。

            
            
                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.

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

            
            
                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不就是用戶數(shù)據(jù)報協(xié)議嘛)是基于數(shù)據(jù)報的,我們并不需要使用一個基于流的socket。創(chuàng)建一個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().

            現(xiàn)在,我們需要接收服務器發(fā)給我們的任何信息接收服務器響應的客戶端節(jié)點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實現(xiàn)一個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.

            創(chuàng)建一個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().

            服務器一直等待直到客戶端發(fā)起同它的連接。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.

            給信息發(fā)送給遠程終端。

            
            
                  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實現(xiàn)一個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.

            創(chuàng)建一個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().

            服務器一直等待直到客戶端發(fā)起同它的連接。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.

            給信息發(fā)送給遠程終端。

            
            
                  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++
            久久久久成人精品无码| 国产精久久一区二区三区| 欧美激情一区二区久久久| 久久亚洲精品无码aⅴ大香| 久久无码高潮喷水| www.久久99| 久久综合五月丁香久久激情| 日韩精品久久久久久久电影| 91精品国产色综合久久| 久久久久国产精品麻豆AR影院 | 亚洲欧美成人综合久久久| 亚洲AV无码久久精品成人| 无码国产69精品久久久久网站| 国产精品无码久久久久久| 欧美久久一区二区三区| 久久久久久无码Av成人影院| 久久五月精品中文字幕| 狠狠色丁香久久综合婷婷| yy6080久久| 久久久久亚洲精品天堂久久久久久| 久久精品国产99久久久古代| 久久av高潮av无码av喷吹| 国产V综合V亚洲欧美久久| 亚洲国产精品嫩草影院久久| 成人精品一区二区久久久| 亚洲国产精品久久电影欧美| 久久久久久一区国产精品| 国产精品久久波多野结衣| 婷婷久久久亚洲欧洲日产国码AV| 久久婷婷色综合一区二区| 国产精久久一区二区三区| 国产一级持黄大片99久久| 少妇内射兰兰久久| 99久久夜色精品国产网站| 亚洲国产精品无码久久青草| 国产精品VIDEOSSEX久久发布| 久久久久免费精品国产| 91精品国产综合久久精品| 久久精品人人做人人爽电影蜜月| 人妻精品久久无码区| 久久久久人妻一区二区三区vr|