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

posts - 6,  comments - 61,  trackbacks - 0

Daytime.4 - A synchronous UDP daytime client

一個(gè)同步的UDP daytime客戶端

This tutorial program shows how to use asio to implement a client application with UDP.

本例示范如何使用Asio實(shí)現(xiàn)一個(gè)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.

 應(yīng)用程序的開始部分同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對(duì)象來找到正確的基于主機(jī)和服務(wù)器名稱的遠(yuǎn)程終端。使用boost::asio::ip::udp::v4()參數(shù)來限制query 僅僅返回IPv4的節(jié)點(diǎn)。


    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() 保證至少返回列表中一個(gè)節(jié)點(diǎn)。這意味著直接解引用(提領(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ù)報(bào)協(xié)議嘛)是基于數(shù)據(jù)報(bào)的,我們并不需要使用一個(gè)基于流的socket。創(chuàng)建一個(gè)boost::asio::ip::udp::socket后即可啟動(dòng)同遠(yuǎn)程終端的連接

    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)在,我們需要接收服務(wù)器發(fā)給我們的任何信息接收服務(wù)器響應(yīng)的客戶端節(jié)點(diǎn)boost::asio::ip::udp::socket::receive_from()啟動(dòng)

    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

一個(gè)同步的UDP daytime服務(wù)器

This tutorial program shows how to use asio to implement a server application with UDP.

本例示范如何使用Asio實(shí)現(xiàn)一個(gè)UDP服務(wù)器應(yīng)用程序。


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)建一個(gè)boost::asio::ip::udp::socket對(duì)象用來接收來自UDP端口13的請(qǐng)求。


 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().

服務(wù)器一直等待直到客戶端發(fā)起同它的連接。remote_endpoint 對(duì)象將被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.

 確定服務(wù)器將要傳回給客戶端的信息。


      std::string message = make_daytime_string();

Send the response to the remote_endpoint.

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


      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

一個(gè)同步的UDP daytime服務(wù)器

This tutorial program shows how to use asio to implement a server application with UDP.

本例示范如何使用Asio實(shí)現(xiàn)一個(gè)UDP服務(wù)器應(yīng)用程序。


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)建一個(gè)boost::asio::ip::udp::socket對(duì)象用來接收來自UDP端口13的請(qǐng)求。


 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().

服務(wù)器一直等待直到客戶端發(fā)起同它的連接。remote_endpoint 對(duì)象將被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.

 確定服務(wù)器將要傳回給客戶端的信息。


      std::string message = make_daytime_string();

Send the response to the remote_endpoint.

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


      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 王曉軒 閱讀(3514) 評(píng)論(2)  編輯 收藏 引用 所屬分類: C\C++

只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            一区二区三区四区五区精品| 亚洲人久久久| 亚洲日本免费电影| 久久电影一区| 国产美女精品视频| 一区二区三区欧美在线观看| 亚洲国产另类精品专区| 久久婷婷蜜乳一本欲蜜臀| 久久综合久久综合九色| 国产一区二区三区四区hd| 欧美伊久线香蕉线新在线| 久久久久五月天| 亚洲黄一区二区三区| 狠色狠色综合久久| 久久婷婷久久| 久久国产直播| 亚洲第一免费播放区| 99在线精品视频在线观看| 欧美午夜女人视频在线| 亚洲中字黄色| 久久婷婷国产综合尤物精品| 欧美一级淫片播放口| 激情av一区二区| 欧美精品自拍偷拍动漫精品| 99国产精品| 久久久久久高潮国产精品视| 亚洲精品国产精品乱码不99| 欧美日韩亚洲天堂| 久久精品国产99| 欧美一区二区三区男人的天堂 | 欧美大秀在线观看| 91久久在线| 欧美中在线观看| 香港成人在线视频| 欧美一区永久视频免费观看| 亚洲国产另类久久精品| 亚洲国产欧美久久| 亚洲精品国产精品国自产在线| 亚洲国产欧美日韩精品| 亚洲国产日韩一区二区| 亚洲激情成人| 夜夜嗨av一区二区三区免费区| 日韩天堂在线观看| 激情综合五月天| 亚洲激情在线观看视频免费| 亚洲美女诱惑| 亚洲国产成人91精品| 国产精品扒开腿爽爽爽视频| 国产精品家教| 欧美精品电影| 久久久国产一区二区| 一本色道久久综合狠狠躁篇怎么玩| 日韩视频专区| 欧美电影在线播放| 欧美在线一二三| 久久精品久久99精品久久| 制服丝袜激情欧洲亚洲| 亚洲国产精品一区二区三区| 亚洲美女黄色片| 亚洲一区三区在线观看| 日韩视频二区| 欧美一级二区| 欧美.日韩.国产.一区.二区| 久久精品二区三区| 欧美激情视频一区二区三区在线播放 | 欧美不卡激情三级在线观看| 欧美日韩国产在线看| 美女国产一区| 久久久久久久久久久一区| 午夜精品视频在线观看| 亚洲视频在线观看视频| 亚洲国产精品成人综合| 欧美www视频在线观看| 亚洲精品一区二区三区婷婷月| 亚洲二区视频在线| 夜夜精品视频| 久久亚洲电影| 国产精品热久久久久夜色精品三区 | 久久精品国产免费看久久精品| 欧美大片18| 午夜精品视频网站| 欧美精品在线视频| 激情视频亚洲| 午夜欧美电影在线观看| 亚洲一区二三| 欧美成人蜜桃| 欧美亚洲一区二区在线观看| 欧美二区在线看| 欧美精品一区在线发布| 国产一区二区电影在线观看| 一区二区三区精品在线| 美日韩精品视频| 欧美激情视频在线播放| 亚洲欧美日韩专区| 久久精品中文字幕免费mv| 国产精品盗摄久久久| 亚洲美女性视频| 麻豆精品精华液| 亚洲激情成人在线| 久久久之久亚州精品露出| 国产精品久久9| 日韩一级在线| 欧美国产日韩一区二区在线观看| 亚洲欧美一区二区三区极速播放 | 亚洲精品欧美日韩专区| 久久久久久9999| 国产欧美精品在线播放| 狠狠久久亚洲欧美专区| 亚洲欧美日韩在线一区| 久久久免费精品视频| 亚洲一区二区3| 欧美在线视频网站| 国产精品影院在线观看| 亚洲一二三区精品| 欧美影院在线| 亚洲自拍偷拍网址| 国产精品啊啊啊| 亚洲午夜在线观看视频在线| 亚洲人成网站精品片在线观看| 久久综合婷婷| 国产精品白丝jk黑袜喷水| 一本久久综合亚洲鲁鲁五月天| 欧美aaa级| 麻豆成人综合网| 亚洲国产成人在线视频| 欧美jizz19性欧美| 久久综合五月| 亚洲激情在线| 亚洲破处大片| 欧美日韩成人在线播放| 亚洲午夜性刺激影院| 日韩亚洲一区在线播放| 欧美视频亚洲视频| …久久精品99久久香蕉国产 | 欧美高清在线精品一区| 看片网站欧美日韩| 亚洲日本中文字幕| 亚洲毛片一区| 国产精品对白刺激久久久| 亚洲你懂的在线视频| 亚洲欧美日韩国产成人精品影院| 国产日韩欧美成人| 亚洲线精品一区二区三区八戒| 99国产精品国产精品毛片| 欧美亚日韩国产aⅴ精品中极品| 亚洲欧美日韩直播| 欧美一区免费视频| 亚洲福利小视频| 亚洲人成高清| 国产精品美女久久久免费| 亚洲欧洲另类| 久久全国免费视频| 久久综合狠狠综合久久综合88| 亚洲美女一区| 亚洲午夜视频| 永久免费毛片在线播放不卡| 久久精品国产一区二区三| 久久久夜夜夜| 一区二区三区精品视频| 亚洲欧美日韩久久精品| 亚洲国产高清自拍| av72成人在线| 国产在线观看91精品一区| 性欧美xxxx视频在线观看| 欧美一区二区三区免费在线看| 亚洲欧洲一二三| 亚洲视频在线免费观看| 禁久久精品乱码| 亚洲另类自拍| 国产亚洲欧洲997久久综合| 欧美激情一区二区三区在线视频观看 | 亚洲一区999| 久久精品国产一区二区三区免费看| 亚洲经典视频在线观看| 亚洲网站在线| 亚洲黄色小视频| 亚洲无毛电影| 亚洲人永久免费| 亚洲欧美日韩精品一区二区| 亚洲第一色在线| 亚洲在线视频观看| 亚洲日本欧美在线| 欧美在线视频观看| 一本一本a久久| 久久久久久久999精品视频| 亚洲天堂激情| 美女脱光内衣内裤视频久久影院| 午夜宅男欧美| 欧美美女福利视频| 免费观看30秒视频久久| 国产精品综合色区在线观看| 亚洲欧洲一区二区三区久久| 激情国产一区| 午夜影院日韩| 亚洲一区免费观看| 欧美激情按摩| 欧美国产精品| 黄色免费成人| 欧美亚洲网站| 欧美一区二区精品在线|