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

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.

 應(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對象來找到正確的基于主機(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() 保證至少返回列表中一個節(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ù)報協(xié)議嘛)是基于數(shù)據(jù)報的,我們并不需要使用一個基于流的socket。創(chuàng)建一個boost::asio::ip::udp::socket后即可啟動同遠(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()啟動

    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服務(wù)器

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

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

服務(wù)器一直等待直到客戶端發(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.

 確定服務(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

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

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

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

服務(wù)器一直等待直到客戶端發(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.

 確定服務(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 王曉軒 閱讀(3516) 評論(2)  編輯 收藏 引用 所屬分類: C\C++

只有注冊用戶登錄后才能發(fā)表評論。
網(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>
            亚洲女人天堂av| 国产精品久久久亚洲一区 | 欧美不卡视频一区发布| 国语自产精品视频在线看| 久久精品一本久久99精品| 久色婷婷小香蕉久久| 在线观看日韩av| 欧美电影免费网站| 一区二区三区精品视频在线观看| 亚洲一区三区视频在线观看| 国产视频久久久久久久| 久久久精品日韩| 亚洲黄网站黄| 午夜精品久久| 伊人久久久大香线蕉综合直播 | 蜜臀久久久99精品久久久久久| 亚洲第一色在线| 亚洲一区精品在线| 好看的日韩视频| 欧美成人精品一区二区| 亚洲香蕉成视频在线观看| 美女精品在线| 亚洲网站啪啪| 激情欧美一区二区三区| 欧美日韩精品免费观看视频| 午夜精品一区二区在线观看| 亚洲风情在线资源站| 亚洲在线第一页| 亚洲第一在线综合网站| 欧美新色视频| 老司机一区二区| 亚洲综合电影| 亚洲精选中文字幕| 麻豆精品视频在线观看视频| 亚洲视频视频在线| 亚洲成人在线网| 国产精品一区一区三区| 欧美精品三级日韩久久| 欧美一级片一区| 一本色道久久88精品综合| 免费在线观看成人av| 亚洲综合色视频| 日韩亚洲综合在线| 狠狠色综合网| 国产欧美日韩一区二区三区在线观看| 欧美不卡视频一区发布| 亚洲欧美综合| 在线一区二区日韩| 亚洲黄色av| 免费久久99精品国产自| 久久精品二区| 亚洲欧美日韩精品在线| 亚洲天堂av在线免费| 亚洲人成在线播放| 精品99一区二区| 国产丝袜一区二区| 国产精品美女| 欧美色综合天天久久综合精品| 免费久久99精品国产自| 久久se精品一区二区| 亚洲主播在线观看| 亚洲一区在线播放| 亚洲视频在线视频| 一区二区三区日韩精品| 亚洲精品国产精品久久清纯直播 | 亚洲精选91| 亚洲国产婷婷综合在线精品| 黑人巨大精品欧美黑白配亚洲| 国产精品免费一区二区三区观看| 欧美日韩在线免费| 欧美日韩美女一区二区| 欧美激情亚洲综合一区| 欧美国产在线观看| 欧美电影电视剧在线观看| 欧美aⅴ99久久黑人专区| 久久免费视频在线观看| 久久婷婷麻豆| 久久色中文字幕| 久久久久在线观看| 免费成人在线视频网站| 久久综合中文| 欧美成年人在线观看| 欧美福利在线| 欧美日韩情趣电影| 欧美性色aⅴ视频一区日韩精品| 欧美日韩在线播放三区| 国产精品av免费在线观看| 国产精品a级| 国产日韩欧美高清免费| 伊人男人综合视频网| 在线电影国产精品| 亚洲精品在线电影| 亚洲视频在线观看免费| 午夜一级久久| 久久先锋资源| 亚洲国产老妈| 一区二区三区视频在线观看| 亚洲综合视频在线| 欧美中文字幕不卡| 欧美va天堂在线| 欧美视频官网| 禁断一区二区三区在线| 亚洲精品裸体| 欧美亚洲一区三区| 免费不卡在线观看av| 91久久精品网| 亚洲在线一区二区| 嫩模写真一区二区三区三州| 欧美日韩亚洲综合在线| 国产欧美在线观看| 亚洲欧洲日夜超级视频| 小嫩嫩精品导航| 免费观看在线综合色| 99国产一区| 久久精品一区| 欧美三日本三级少妇三2023| 国产区二精品视| 亚洲伦伦在线| 久久婷婷丁香| 99精品欧美一区| 久久久久久国产精品一区| 欧美日韩国产在线播放| 国产一区亚洲| 亚洲综合不卡| 亚洲国产精品ⅴa在线观看| 亚洲一区国产一区| 欧美激情免费在线| 伊人伊人伊人久久| 亚洲综合国产激情另类一区| 欧美激情在线观看| 午夜亚洲性色视频| 欧美日韩一区二区三区在线视频| 国内自拍亚洲| 亚洲欧美日韩另类精品一区二区三区| 欧美激情网友自拍| 午夜视频在线观看一区| 欧美日韩在线观看视频| 亚洲三级视频| 美女精品视频一区| 欧美一级视频精品观看| 国产精品v欧美精品v日本精品动漫| 亚洲第一精品夜夜躁人人躁| 久久国产精品久久国产精品| 日韩亚洲国产精品| 免费观看不卡av| 在线精品国产成人综合| 久久精品在线观看| 亚洲欧美高清| 国产精品久久97| 亚洲先锋成人| 亚洲巨乳在线| 欧美精品1区| 亚洲日本欧美日韩高观看| 蜜月aⅴ免费一区二区三区| 新67194成人永久网站| 国产精品久久久久久影视| 一区二区三区四区五区视频| 亚洲国产日韩在线| 免费中文字幕日韩欧美| 亚洲人成7777| 欧美第一黄网免费网站| 久久久夜夜夜| 在线国产欧美| 免费看精品久久片| 久久一区二区三区国产精品| 精品96久久久久久中文字幕无| 久久九九国产精品怡红院| 香蕉久久精品日日躁夜夜躁| 国产伦精品一区二区三区视频孕妇| 亚洲欧美精品suv| 亚洲视频在线观看免费| 国产精品久久久久久久浪潮网站| 午夜精品一区二区三区电影天堂| av不卡免费看| 国产精品午夜久久| 久久国产精品久久久| 欧美亚洲免费在线| 伊人久久噜噜噜躁狠狠躁| 欧美成人综合| 欧美日韩精品免费观看视一区二区| 亚洲深夜影院| 欧美一级片一区| 在线日韩av| 91久久国产综合久久91精品网站| 欧美精品成人91久久久久久久| 在线性视频日韩欧美| 亚洲性线免费观看视频成熟| 国产网站欧美日韩免费精品在线观看| 久久久久天天天天| 久久男人资源视频| 在线视频精品| 欧美亚洲免费电影| 91久久久在线| 亚洲手机视频| 樱桃国产成人精品视频| 亚洲精品日韩一| 国产日韩精品在线| 欧美成人在线免费视频| 欧美图区在线视频| 久久亚洲二区|