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

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>
            欧美不卡福利| 亚洲国产一区二区三区青草影视| 亚洲精品国产日韩| 欧美日韩免费一区二区三区视频| 亚洲高清av在线| 久久视频精品在线| 欧美成黄导航| 美女精品在线观看| 一区二区三区精品视频在线观看| 亚洲视频每日更新| 国产一区白浆| 亚洲日本黄色| 国产日韩一区二区三区| 欧美成年人视频| 国产精品成人午夜| 免费国产一区二区| 欧美午夜女人视频在线| 老司机成人网| 欧美午夜在线| 欧美福利视频一区| 国产精品久久久久aaaa樱花| 欧美成人黑人xx视频免费观看| 欧美午夜精彩| 欧美大片91| 国产拍揄自揄精品视频麻豆| 亚洲国产精品成人一区二区| 国产一区二区高清不卡| 亚洲精品一区中文| 最新日韩中文字幕| 中文欧美在线视频| 亚洲国产日韩一区二区| 亚洲欧美日韩精品| 日韩一级大片| 久久久午夜精品| 亚洲欧美另类在线观看| 蜜桃久久av一区| 午夜精品久久久久久久久久久久| 久久都是精品| 午夜视黄欧洲亚洲| 欧美精品在线网站| 免费欧美在线视频| 国产欧美日韩麻豆91| 欧美国产欧美亚洲国产日韩mv天天看完整 | 久久精品国产69国产精品亚洲| 麻豆久久精品| 欧美中文字幕在线播放| 欧美日本国产在线| 欧美大胆成人| 在线免费观看一区二区三区| 久久国产精品网站| 久久精品国产99国产精品| 国产精品丝袜白浆摸在线| 夜夜爽www精品| 在线亚洲精品| 欧美日韩在线精品| 亚洲理论在线观看| 国产精品99久久久久久有的能看| 欧美精品成人一区二区在线观看| 免费久久99精品国产自| 国产在线观看一区| 久久国内精品自在自线400部| 欧美一区激情| 国产精品久久久久影院亚瑟| 日韩亚洲欧美一区| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 欧美夫妇交换俱乐部在线观看| 黄色国产精品一区二区三区| 久久高清国产| 欧美**字幕| 亚洲精品久久久久久久久| 欧美顶级艳妇交换群宴| 亚洲精品日韩欧美| 亚洲一区二区三区在线看| 国产精品久久久| 亚洲女人天堂av| 久久久久久久久久久久久9999| 激情小说另类小说亚洲欧美| 另类春色校园亚洲| 亚洲精品在线视频观看| 午夜亚洲精品| 精品动漫一区二区| 欧美精品久久久久久久| 亚洲天堂偷拍| 久久久久久久久伊人| 亚洲激情视频网| 国产精品国产一区二区| 久久高清一区| 亚洲精品国精品久久99热一| 欧美伊人久久| 亚洲伦理网站| 国产精品自在线| 欧美aa在线视频| 亚洲性视频网站| 欧美成人在线免费视频| 亚洲女性裸体视频| 亚洲国产电影| 国产伦精品一区二区三区在线观看| 久久国产精品一区二区三区四区| 91久久精品一区| 久久成人免费电影| 夜色激情一区二区| 韩国一区二区三区美女美女秀| 欧美极品在线观看| 欧美专区福利在线| 99精品国产高清一区二区| 蜜桃av噜噜一区二区三区| 亚洲影视在线播放| 亚洲人成网站影音先锋播放| 国产色爱av资源综合区| 欧美日韩成人在线观看| 久久久久久亚洲精品杨幂换脸| 99国产精品久久久久久久久久| 另类av一区二区| 午夜精品国产更新| 一本色道久久99精品综合| 黄色在线成人| 国产亚洲精品aa午夜观看| 欧美色图天堂网| 欧美粗暴jizz性欧美20| 久久人91精品久久久久久不卡| 亚洲与欧洲av电影| 99精品热视频| 亚洲图片在线| 国产人成精品一区二区三| 欧美人与禽猛交乱配视频| 久久久久久久久岛国免费| 亚洲欧美三级伦理| 亚洲素人一区二区| 一区二区三区免费网站| 99视频精品免费观看| 91久久黄色| 亚洲成在线观看| 欧美护士18xxxxhd| 欧美国产另类| 欧美激情第六页| 欧美成人官网二区| 欧美激情在线狂野欧美精品| 免费成人小视频| 欧美fxxxxxx另类| 欧美成人中文字幕在线| 亚洲电影免费在线| 亚洲国语精品自产拍在线观看| 亚洲福利视频一区二区| 亚洲日本理论电影| 一区二区高清| 午夜激情亚洲| 久久久国产一区二区| 蜜桃av综合| 欧美日韩亚洲91| 国产精品乱码妇女bbbb| 国产精品日日摸夜夜添夜夜av| 国产女精品视频网站免费| 国产在线观看精品一区二区三区| 国产一区二区三区在线观看免费视频| 国内外成人免费激情在线视频网站| 精品动漫一区| 亚洲美女视频网| 亚洲免费中文字幕| 久久亚洲风情| 亚洲人成毛片在线播放女女| 一区二区三区视频在线| 亚洲一区二区在线| 久久久国产精品一区二区中文 | 日韩视频不卡| 亚洲一区二区三区在线| 久久久99久久精品女同性 | 亚洲男人影院| 久久先锋资源| 国产精品成人观看视频免费| 国产视频精品免费播放| 亚洲黑丝在线| 欧美亚洲午夜视频在线观看| 蜜臀久久99精品久久久久久9 | 一区二区三区精品久久久| 欧美一区二区三区视频免费播放| 欧美国产综合| 国产一区白浆| 在线一区视频| 欧美成人免费播放| 亚洲一区二区视频在线| 欧美高清在线精品一区| 国产午夜精品久久久久久免费视| 亚洲美女精品一区| 久久亚洲不卡| 亚洲午夜电影网| 欧美大片免费看| 国产亚洲欧美日韩精品| 亚洲午夜久久久久久尤物| 欧美凹凸一区二区三区视频| 亚洲自拍偷拍视频| 欧美日韩高清区| 黄色一区三区| 久久精品国产2020观看福利| 在线视频你懂得一区| 免费观看一区| 亚洲成人中文| 噜噜噜躁狠狠躁狠狠精品视频| 午夜国产精品视频| 国产精品久久久久秋霞鲁丝| 一本综合久久|