• <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 王曉軒 閱讀(3473) 評論(2)  編輯 收藏 引用 所屬分類: C\C++
            国产精品美女久久福利网站| 国产精品VIDEOSSEX久久发布| 久久美女人爽女人爽| 91久久精品国产91性色也| 亚洲综合精品香蕉久久网97| 国产精品99久久精品| 精品一久久香蕉国产线看播放| 热综合一本伊人久久精品| 久久99热这里只有精品66| 欧洲精品久久久av无码电影| yellow中文字幕久久网| 久久久久久久免费视频| 99久久国产亚洲高清观看2024 | 久久精品这里只有精99品| 久久青青色综合| 色综合久久中文色婷婷| 亚洲午夜无码久久久久| 日本久久久久久中文字幕| 久久九九久精品国产免费直播| 久久久老熟女一区二区三区| 久久久网中文字幕| 日本福利片国产午夜久久| 久久久久女教师免费一区| 久久丫精品国产亚洲av不卡| 久久国产亚洲精品| 久久亚洲AV无码西西人体| 久久久精品免费国产四虎| 97久久国产露脸精品国产| 久久99精品国产99久久| 69久久夜色精品国产69| 亚洲精品乱码久久久久久自慰| 欧美精品一区二区精品久久| 韩国免费A级毛片久久| 伊人久久大香线蕉av不变影院| 久久久久久狠狠丁香| 日日噜噜夜夜狠狠久久丁香五月| 久久精品视频一| 久久综合亚洲色一区二区三区| 国产巨作麻豆欧美亚洲综合久久| 久久99国产精品久久久| 久久美女网站免费|