青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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 王曉軒 閱讀(3502) 評論(2)  編輯 收藏 引用 所屬分類: C\C++
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美与黑人午夜性猛交久久久| 欧美激情中文不卡| 日韩网站在线观看| 久久精品亚洲精品国产欧美kt∨| 在线亚洲一区二区| 麻豆久久精品| 猫咪成人在线观看| 国产亚洲综合精品| 午夜精品久久久久久久99水蜜桃 | 国产精品视频第一区| 欧美韩日一区| 在线精品一区二区| 久久久久国色av免费观看性色| 性欧美大战久久久久久久免费观看| 欧美乱人伦中文字幕在线| 欧美成人中文字幕| 在线日韩欧美| 久久噜噜噜精品国产亚洲综合 | 欧美在线视频免费| 国产精品三级视频| 亚洲欧美三级伦理| 欧美一区三区三区高中清蜜桃 | 国产精品久久久久久影视 | 一区二区三区四区五区在线| 亚洲美女一区| 欧美精品色综合| 亚洲精品一二| 亚洲一区二区三区乱码aⅴ| 欧美日韩性生活视频| av不卡在线观看| 亚洲伊人伊色伊影伊综合网| 国产精品久久久久9999高清 | 亚洲曰本av电影| 欧美一级网站| 国产亚洲综合在线| 久久久国产成人精品| 欧美69wwwcom| 99国内精品久久| 欧美视频免费看| 亚洲欧美日韩国产成人精品影院| 久久大香伊蕉在人线观看热2| 国产欧美亚洲日本| 久久久久亚洲综合| 亚洲国产精品久久久久婷婷老年 | 美女被久久久| 亚洲高清视频一区二区| 一本色道久久| 国产精品久久久久久久午夜片| 性欧美xxxx视频在线观看| 久久在线免费观看| 日韩视频在线观看| 国产毛片精品国产一区二区三区| 久久看片网站| 99国产精品视频免费观看一公开 | 欧美综合国产精品久久丁香| 精品51国产黑色丝袜高跟鞋| 欧美高清你懂得| 亚洲一区二区免费| 欧美刺激性大交免费视频| 在线午夜精品自拍| 国产在线精品成人一区二区三区| 欧美成黄导航| 亚洲欧美日韩成人| 亚洲国产一区二区三区高清| 小嫩嫩精品导航| 亚洲经典自拍| 国产亚洲午夜高清国产拍精品| 噜噜噜91成人网| 亚洲在线视频观看| 亚洲精品1234| 久久久久女教师免费一区| 一本一道久久综合狠狠老精东影业| 国产日韩精品视频一区二区三区 | 欧美国产国产综合| 欧美亚洲一区在线| 一本色道88久久加勒比精品| 欧美成人亚洲| 久久久久久97三级| 亚洲欧美日韩在线一区| 亚洲区欧美区| 精品999久久久| 国产欧美亚洲日本| 国产精品www色诱视频| 欧美高潮视频| 免费观看30秒视频久久| 欧美亚洲一区| 亚洲一区欧美激情| 一本不卡影院| 亚洲理伦电影| 亚洲国产日韩精品| 欧美电影免费观看| 麻豆久久久9性大片| 久久久久国产免费免费| 欧美在线精品免播放器视频| 亚洲午夜精品福利| 在线视频精品一区| 在线综合亚洲欧美在线视频| 亚洲精品久久久久久一区二区| 在线播放日韩专区| 国产午夜亚洲精品羞羞网站| 国产精品久久久一区二区三区| 欧美日韩精品在线| 欧美日韩免费区域视频在线观看| 欧美h视频在线| 免费在线亚洲| 欧美黄色aa电影| 欧美国产日韩一区二区三区| 欧美成人综合| 欧美国产综合| 欧美日韩一区二区三区四区五区| 欧美精品一区在线观看| 欧美精品久久久久久久久久| 欧美黄色aaaa| 欧美视频中文在线看 | 亚洲精品乱码视频 | 亚洲国产精品尤物yw在线观看| 精品福利电影| 亚洲经典在线| 一区二区精品在线观看| 亚洲图片欧洲图片av| 午夜精品久久久久久| 久久不射2019中文字幕| 久久一区二区三区四区| 亚洲电影免费在线| 亚洲精品欧洲| 亚洲一区二区三区中文字幕在线| 午夜精品久久| 免费在线观看日韩欧美| 欧美精品一区二区三区高清aⅴ| 欧美视频导航| 国内精品久久久久影院优| 亚洲激情黄色| 亚洲免费在线电影| 久久九九免费| 亚洲精华国产欧美| 亚洲欧美日韩中文视频| 久久久久国产免费免费| 欧美日韩国产三级| 国产欧美综合一区二区三区| 在线看一区二区| 亚洲视屏在线播放| 久久久不卡网国产精品一区| 欧美韩日高清| 香蕉久久一区二区不卡无毒影院| 久久亚洲精品中文字幕冲田杏梨| 欧美日韩国产一级片| 国产婷婷一区二区| aa级大片欧美| 久久人人看视频| 99国产精品久久久久久久成人热| 欧美一区二区三区啪啪| 欧美精品一区二区视频| 国产在线播精品第三| 制服丝袜激情欧洲亚洲| 久久亚洲私人国产精品va| 999在线观看精品免费不卡网站| 午夜一级久久| 欧美日韩一区在线播放| 136国产福利精品导航| 午夜亚洲视频| 亚洲国产精品综合| 久久精品观看| 国产精品高清免费在线观看| 亚洲国产婷婷综合在线精品 | 在线视频日本亚洲性| 久久亚洲精品欧美| 国产亚洲午夜| 亚洲影视在线| 亚洲精品资源| 免费成人在线视频网站| 国模精品一区二区三区| 亚洲制服av| 亚洲精品日本| 欧美成人免费视频| 1769国内精品视频在线播放| 欧美一区久久| 亚洲砖区区免费| 国产精品久久久久影院色老大 | 亚洲黄色在线看| 久久夜色精品亚洲噜噜国产mv| 亚洲一区三区电影在线观看| 欧美日韩视频一区二区三区| 亚洲日韩第九十九页| 欧美丰满高潮xxxx喷水动漫| 久久国产主播精品| 国产视频一区免费看| 欧美一区二区三区免费视| 亚洲素人在线| 国产精品美女久久久免费| 亚洲私人影院在线观看| 日韩天堂在线观看| 欧美三级网页| 亚洲欧美激情视频| 亚洲综合色丁香婷婷六月图片| 欧美日韩影院| 午夜精品久久久久久久久| 亚洲在线成人| 国产午夜精品在线观看| 久久人人爽国产| 美日韩精品视频免费看|