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

大龍的博客

常用鏈接

統(tǒng)計(jì)

最新評(píng)論

BOOST 絕對(duì)實(shí)用手冊(cè)(來(lái)自網(wǎng)絡(luò))

張沈鵬     電子科技大學(xué)大三        生物醫(yī)學(xué)工程

我的C++Blog 我的文學(xué)Blog

更新:2006.10 beta

參考:BOOST文檔

  • -- 歡迎轉(zhuǎn)載,但請(qǐng)保留引用網(wǎng)址以獲得更新

1. 序言

現(xiàn)在學(xué)的東西很容易忘記,寫(xiě)這篇文章的目的是能讓我在需要時(shí)快速找回當(dāng)時(shí)的感覺(jué). Let's BOOST THE WORLD .

2. 編譯:VC2005注意

在 屬性->C/C++->預(yù)處理器->預(yù)處理定義 中加入

_CRT_SECURE_NO_DEPRECATE;

來(lái)屏蔽不必要的警告

3. Asio 網(wǎng)絡(luò)庫(kù)

Boost.Asio是利用當(dāng)代C++的先進(jìn)方法,跨平臺(tái),異步I/O模型的C++網(wǎng)絡(luò)庫(kù).

3.1. 網(wǎng)絡(luò)庫(kù):VC2005注意

在 屬性->C/C++->命令行 中加入

-DBOOST_REGEX_NO_LIB

來(lái)防止自動(dòng)連接.

3.2. 同步Timer

本章介紹asio如何在定時(shí)器上進(jìn)行阻塞等待(blocking wait).

實(shí)現(xiàn),我們包含必要的頭文件.

所有的asio類(lèi)可以簡(jiǎn)單的通過(guò)include "asio.hpp"來(lái)調(diào)用.

#include <iostream>
#include <boost/asio.hpp>

此外,這個(gè)示例用到了timer,我們還要包含Boost.Date_Time的頭文件來(lái)控制時(shí)間.

#include <boost/date_time/posix_time/posix_time.hpp>

使用asio至少需要一個(gè)boost::asio::io_service對(duì)象.該類(lèi)提供了訪(fǎng)問(wèn)I/O的功能.我們首先在main函數(shù)中聲明它.

int main()
{
boost::asio::io_service io;

下一步我們聲明boost::asio::deadline_timer對(duì)象.這個(gè)asio的核心類(lèi)提供I/O的功能(這里更確切的說(shuō)是定時(shí)功能),總是把一個(gè)io_service對(duì)象作為他的第一個(gè)構(gòu)造函數(shù),而第二個(gè)構(gòu)造函數(shù)的參數(shù)設(shè)定timer會(huì)在5秒后到時(shí)(expired).

boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));

這個(gè)簡(jiǎn)單的示例中我們演示了定時(shí)器上的一個(gè)阻塞等待.就是說(shuō),調(diào)用boost::asio::deadline_timer::wait()的在創(chuàng)建后5秒內(nèi)(注意:不是等待開(kāi)始后),timer到時(shí)之前不會(huì)返回任何值.

一個(gè)deadline_timer只有兩種狀態(tài):到時(shí),未到時(shí).

如果boost::asio::deadline_timer::wait()在到時(shí)的timer對(duì)象上調(diào)用,會(huì)立即return.

t.wait();

最后,我們輸出理所當(dāng)然的"Hello, world!"來(lái)演示timer到時(shí)了.

std::cout << "Hello, world!\n";
return 0;
}

完整的代碼:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world!\n";
return 0;
}

3.3. 異步Timer

#include <iostream>
#include <asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

asio的異步函數(shù)會(huì)在一個(gè)異步操作完成后被回調(diào).這里我們定義了一個(gè)將被回調(diào)的函數(shù).

void print(const asio::error& /*e*/)
{
  std::cout << "Hello, world!\n";
}
int main()
{
  asio::io_service io;
  asio::deadline_timer t(io, boost::posix_time::seconds(5));

這里我們調(diào)用asio::deadline_timer::async_wait()來(lái)異步等待

  t.async_wait(print);

最后,我們必須調(diào)用asio::io_service::run().

asio庫(kù)只會(huì)調(diào)用那個(gè)正在運(yùn)行的asio::io_service::run()的回調(diào)函數(shù).

如果asio::io_service::run()不被調(diào)用,那么回調(diào)永遠(yuǎn)不會(huì)發(fā)生.

asio::io_service::run()會(huì)持續(xù)工作到點(diǎn),這里就是timer到時(shí),回調(diào)完成.

別忘了在調(diào)用 asio::io_service::run()之前設(shè)置好io_service的任務(wù).比如,這里,如果我們忘記先調(diào)用asio::deadline_timer::async_wait()則asio::io_service::run()會(huì)在瞬間return.

  io.run();
  return 0;
}

完整的代碼:

#include <iostream>
#include <asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const asio::error& /*e*/)
{
  std::cout << "Hello, world!\n";
}
int main()
{
  asio::io_service io;
  asio::deadline_timer t(io, boost::posix_time::seconds(5));
  t.async_wait(print);
  io.run();
  return 0;
}

3.4. 回調(diào)函數(shù)的參數(shù)

這里我們將每秒回調(diào)一次,來(lái)演示如何回調(diào)函數(shù)參數(shù)的含義

#include <iostream>
#include <asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

首先,調(diào)整一下timer的持續(xù)時(shí)間,開(kāi)始一個(gè)異步等待.顯示,回調(diào)函數(shù)需要訪(fǎng)問(wèn)timer來(lái)實(shí)現(xiàn)周期運(yùn)行,所以我們?cè)俳榻B兩個(gè)新參數(shù)

  • 指向timer的指針
  • 一個(gè)int*來(lái)指向計(jì)數(shù)器

void print(const asio::error& /*e*/,
    asio::deadline_timer* t, int* count)
{

我們打算讓這個(gè)函數(shù)運(yùn)行6個(gè)周期,然而你會(huì)發(fā)現(xiàn)這里沒(méi)有顯式的方法來(lái)終止io_service.不過(guò),回顧上一節(jié),你會(huì)發(fā)現(xiàn)當(dāng)asio::io_service::run()會(huì)在所有任務(wù)完成時(shí)終止.這樣我們當(dāng)計(jì)算器的值達(dá)到5時(shí)(0為第一次運(yùn)行的值),不再開(kāi)啟一個(gè)新的異步等待就可以了.

  if (*count < 5)
  {
    std::cout << *count << "\n";
    ++(*count);

然后,我們推遲的timer的終止時(shí)間.通過(guò)在原先的終止時(shí)間上增加延時(shí),我們可以確保timer不會(huì)在處理回調(diào)函數(shù)所需時(shí)間內(nèi)的到期.

(原文:By calculating the new expiry time relative to the old, we can ensure that the timer does not drift away from the whole-second mark due to any delays in processing the handler.)

    t->expires_at(t->expires_at() + boost::posix_time::seconds(1));

然后我們開(kāi)始一個(gè)新的同步等待.如您所見(jiàn),我們用把print和他的多個(gè)參數(shù)用boost::bind函數(shù)合成一個(gè)的形為void(const asio::error&)回調(diào)函數(shù)(準(zhǔn)確的說(shuō)是function object).

在這個(gè)例子中, boost::bind的asio::placeholders::error參數(shù)是為了給回調(diào)函數(shù)傳入一個(gè)error對(duì)象.當(dāng)進(jìn)行一個(gè)異步操作,開(kāi)始boost::bind時(shí),你需要使用它來(lái)匹配回調(diào)函數(shù)的參數(shù)表.下一節(jié)中你會(huì)學(xué)到回調(diào)函數(shù)不需要error參數(shù)時(shí)可以省略它.

    t->async_wait(boost::bind(print,
          asio::placeholders::error, t, count));
  }
}
int main()
{
  asio::io_service io;
  int count = 0;
  asio::deadline_timer t(io, boost::posix_time::seconds(1));

和上面一樣,我們?cè)僖淮问褂昧私壎╝sio::deadline_timer::async_wait()

  t.async_wait(boost::bind(print,
        asio::placeholders::error, &t, &count));
  io.run();

在結(jié)尾,我們打印出的最后一次沒(méi)有設(shè)置timer的調(diào)用的count的值

  std::cout << "Final count is " << count << "\n";
  return 0;
}

完整的代碼:

#include <iostream>
#include <asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const asio::error& /*e*/,
    asio::deadline_timer* t, int* count)
{
  if (*count < 5)
  {
    std::cout << *count << "\n";
    ++(*count);
    t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
    t->async_wait(boost::bind(print,
          asio::placeholders::error, t, count));
  }
}
int main()
{
  asio::io_service io;
  int count = 0;
  asio::deadline_timer t(io, boost::posix_time::seconds(1));
  t.async_wait(boost::bind(print,
        asio::placeholders::error, &t, &count));
  io.run();
  std::cout << "Final count is " << count << "\n";
  return 0;
}

3.5. 成員函數(shù)作為回調(diào)函數(shù)

本例的運(yùn)行結(jié)果和上一節(jié)類(lèi)似

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

我們先定義一個(gè)printer類(lèi)

class printer
{
public:

構(gòu)造函數(shù)有一個(gè)io_service參數(shù),并且在初始化timer_時(shí)用到了它.用來(lái)計(jì)數(shù)的count_這里同樣作為了成員變量

  printer(boost::asio::io_service& io)
    : timer_(io, boost::posix_time::seconds(1)),
      count_(0)
  {

boost::bind同樣可以出色的工作在成員函數(shù)上.眾所周知,所有的非靜態(tài)成員函數(shù)都有一個(gè)隱式的this參數(shù),我們需要把this作為參數(shù)bind到成員函數(shù)上.和上一節(jié)類(lèi)似,我們?cè)俅斡胋ind構(gòu)造出void(const boost::asio::error&)形式的函數(shù).

注意,這里沒(méi)有指定boost::asio::placeholders::error占位符,因?yàn)檫@個(gè)print成員函數(shù)沒(méi)有接受一個(gè)error對(duì)象作為參數(shù).

    timer_.async_wait(boost::bind(&printer::print, this));
  }

在類(lèi)的折構(gòu)函數(shù)中我們輸出最后一次回調(diào)的conut的值

  ~printer()
  {
    std::cout << "Final count is " << count_ << "\n";
  }

print函數(shù)于上一節(jié)的十分類(lèi)似,但是用成員變量取代了參數(shù).

  void print()
  {
    if (count_ < 5)
    {
      std::cout << count_ << "\n";
      ++count_;
      timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
      timer_.async_wait(boost::bind(&printer::print, this));
    }
  }
private:
  boost::asio::deadline_timer timer_;
  int count_;
};

現(xiàn)在main函數(shù)清爽多了,在運(yùn)行io_service之前只需要簡(jiǎn)單的定義一個(gè)printer對(duì)象.

int main()
{
  boost::asio::io_service io;
  printer p(io);
  io.run();
  return 0;
}

完整的代碼:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
  printer(boost::asio::io_service& io)
    : timer_(io, boost::posix_time::seconds(1)),
      count_(0)
  {
    timer_.async_wait(boost::bind(&printer::print, this));
  }
  ~printer()
  {
    std::cout << "Final count is " << count_ << "\n";
  }
  void print()
  {
    if (count_ < 5)
    {
      std::cout << count_ << "\n";
      ++count_;
      timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
      timer_.async_wait(boost::bind(&printer::print, this));
    }
  }
private:
  boost::asio::deadline_timer timer_;
  int count_;
};
int main()
{
  boost::asio::io_service io;
  printer p(io);
  io.run();
  return 0;
}

3.6. 多線(xiàn)程回調(diào)同步

本節(jié)演示了使用boost::asio::strand在多線(xiàn)程程序中進(jìn)行回調(diào)同步(synchronise).

先前的幾節(jié)闡明了如何在單線(xiàn)程程序中用boost::asio::io_service::run()進(jìn)行同步.如您所見(jiàn),asio庫(kù)確保 僅當(dāng) 當(dāng)前線(xiàn)程調(diào)用boost::asio::io_service::run()時(shí)產(chǎn)生回調(diào).顯然,僅在一個(gè)線(xiàn)程中調(diào)用boost::asio::io_service::run() 來(lái)確保回調(diào)是適用于并發(fā)編程的.

一個(gè)基于asio的程序最好是從單線(xiàn)程入手,但是單線(xiàn)程有如下的限制,這一點(diǎn)在服務(wù)器上尤其明顯:

  • 當(dāng)回調(diào)耗時(shí)較長(zhǎng)時(shí),反應(yīng)遲鈍.
  • 在多核的系統(tǒng)上無(wú)能為力

如果你發(fā)覺(jué)你陷入了這種困擾,可以替代的方法是建立一個(gè)boost::asio::io_service::run()的線(xiàn)程池.然而這樣就允許回調(diào)函數(shù)并發(fā)執(zhí)行.所以,當(dāng)回調(diào)函數(shù)需要訪(fǎng)問(wèn)一個(gè)共享,線(xiàn)程不安全的資源時(shí),我們需要一種方式來(lái)同步操作.

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

在上一節(jié)的基礎(chǔ)上我們定義一個(gè)printer類(lèi),此次,它將并行運(yùn)行兩個(gè)timer

class printer
{
public:

除了聲明了一對(duì)boost::asio::deadline_timer,構(gòu)造函數(shù)也初始化了類(lèi)型為boost::asio::strand的strand_成員.

boost::asio::strand可以分配的回調(diào)函數(shù).它保證無(wú)論有多少線(xiàn)程調(diào)用了boost::asio::io_service::run(),下一個(gè)回調(diào)函數(shù)僅在前一個(gè)回調(diào)函數(shù)完成后開(kāi)始,當(dāng)然回調(diào)函數(shù)仍然可以和那些不使用boost::asio::strand分配,或是使用另一個(gè)boost::asio::strand分配的回調(diào)函數(shù)一起并發(fā)執(zhí)行.

  printer(boost::asio::io_service& io)
    : strand_(io),
      timer1_(io, boost::posix_time::seconds(1)),
      timer2_(io, boost::posix_time::seconds(1)),
      count_(0)
  {

當(dāng)一個(gè)異步操作開(kāi)始時(shí),用boost::asio::strand來(lái) "wrapped(包裝)"回調(diào)函數(shù).boost::asio::strand::wrap()會(huì)返回一個(gè)由boost::asio::strand分配的新的handler(句柄),這樣,我們可以確保它們不會(huì)同時(shí)運(yùn)行.

    timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
    timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
  }
  ~printer()
  {
    std::cout << "Final count is " << count_ << "\n";
  }

多線(xiàn)程程序中,回調(diào)函數(shù)在訪(fǎng)問(wèn)共享資源前需要同步.這里共享資源是std::cout 和count_變量.

  void print1()
  {
    if (count_ < 10)
    {
      std::cout << "Timer 1: " << count_ << "\n";
      ++count_;
      timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
      timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
    }
  }
  void print2()
  {
    if (count_ < 10)
    {
      std::cout << "Timer 2: " << count_ << "\n";
      ++count_;
      timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
      timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
    }
  }
private:
  boost::asio::strand strand_;
  boost::asio::deadline_timer timer1_;
  boost::asio::deadline_timer timer2_;
  int count_;
};

main函數(shù)中boost::asio::io_service::run()在兩個(gè)線(xiàn)程中被調(diào)用:主線(xiàn)程,一個(gè)boost::thread線(xiàn)程.

正如單線(xiàn)程中那樣,并發(fā)的boost::asio::io_service::run()會(huì)一直運(yùn)行直到完成任務(wù).后臺(tái)的線(xiàn)程將在所有異步線(xiàn)程完成后終結(jié).

int main()
{
  boost::asio::io_service io;
  printer p(io);
  boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
  io.run();
  t.join();
  return 0;
}

完整的代碼:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
  printer(boost::asio::io_service& io)
    : strand_(io),
      timer1_(io, boost::posix_time::seconds(1)),
      timer2_(io, boost::posix_time::seconds(1)),
      count_(0)
  {
    timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
    timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
  }
  ~printer()
  {
    std::cout << "Final count is " << count_ << "\n";
  }
  void print1()
  {
    if (count_ < 10)
    {
      std::cout << "Timer 1: " << count_ << "\n";
      ++count_;
      timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
      timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
    }
  }
  void print2()
  {
    if (count_ < 10)
    {
      std::cout << "Timer 2: " << count_ << "\n";
      ++count_;
      timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
      timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
    }
  }
private:
  boost::asio::strand strand_;
  boost::asio::deadline_timer timer1_;
  boost::asio::deadline_timer timer2_;
  int count_;
};
int main()
{
  boost::asio::io_service io;
  printer p(io);
  boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
  io.run();
  t.join();
  return 0;
}

3.7. TCP客戶(hù)端:對(duì)準(zhǔn)時(shí)間

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

本程序的目的是訪(fǎng)問(wèn)一個(gè)時(shí)間同步服務(wù)器,我們需要用戶(hù)指定一個(gè)服務(wù)器(如time-nw.nist.gov),用IP亦可.

(譯者注:日期查詢(xún)協(xié)議,這種時(shí)間傳輸協(xié)議不指定固定的傳輸格式,只要求按照ASCII標(biāo)準(zhǔn)發(fā)送數(shù)據(jù)。)

				
				using boost::asio::ip::tcp;
int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: client <host>" << std::endl;
      return 1;
    }

用asio進(jìn)行網(wǎng)絡(luò)連接至少需要一個(gè)boost::asio::io_service對(duì)象

    boost::asio::io_service io_service;

我們需要把在命令行參數(shù)中指定的服務(wù)器轉(zhuǎn)換為T(mén)CP上的節(jié)點(diǎn).完成這項(xiàng)工作需要boost::asio::ip::tcp::resolver對(duì)象

    tcp::resolver resolver(io_service);

一個(gè)resolver對(duì)象查詢(xún)一個(gè)參數(shù),并將其轉(zhuǎn)換為T(mén)CP上節(jié)點(diǎn)的列表.這里我們把a(bǔ)rgv[1]中的sever的名字和要查詢(xún)字串daytime關(guān)聯(lián).

    tcp::resolver::query query(argv[1], "daytime");

節(jié)點(diǎn)列表可以用 boost::asio::ip::tcp::resolver::iterator 來(lái)進(jìn)行迭代.iterator默認(rèn)的構(gòu)造函數(shù)生成一個(gè)end iterator.

    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
    tcp::resolver::iterator end;

現(xiàn)在我們建立一個(gè)連接的sockert,由于獲得節(jié)點(diǎn)既有IPv4也有IPv6的.所以,我們需要依次嘗試他們直到找到一個(gè)可以正常工作的.這步使得我們的程序獨(dú)立于IP版本

    tcp::socket socket(io_service);
    boost::asio::error error = boost::asio::error::host_not_found;
    while (error && endpoint_iterator != end)
    {
      socket.close();
      socket.connect(*endpoint_iterator++, boost::asio::assign_error(error));
    }
    if (error)
      throw error;

連接完成,我們需要做的是讀取daytime服務(wù)器的響應(yīng).

我們用boost::array來(lái)保存得到的數(shù)據(jù),boost::asio::buffer()會(huì)自動(dòng)根據(jù)array的大小暫停工作,來(lái)防止緩沖溢出.除了使用boost::array,也可以使用char [] 或std::vector.

    for (;;)
    {
      boost::array<char, 128> buf;
      boost::asio::error error;
      size_t len = socket.read_some(
          boost::asio::buffer(buf), boost::asio::assign_error(error));

當(dāng)服務(wù)器關(guān)閉連接時(shí),boost::asio::ip::tcp::socket::read_some()會(huì)用boost::asio::error::eof標(biāo)志完成, 這時(shí)我們應(yīng)該退出讀取循環(huán)了.

      if (error == boost::asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw error; // Some other error.
      std::cout.write(buf.data(), len);
    }

如果發(fā)生了什么異常我們同樣會(huì)拋出它

  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

運(yùn)行示例:在windowsXP的cmd窗口下

輸入:upload.exe time-a.nist.gov

輸出:54031 06-10-23 01:50:45 07 0 0 454.2 UTC(NIST) *

完整的代碼:

#include <iostream>
#include <boost/array.hpp>
#include <asio.hpp>
using asio::ip::tcp;
int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: client <host>" << std::endl;
      return 1;
    }
    asio::io_service io_service;
    tcp::resolver resolver(io_service);
    tcp::resolver::query query(argv[1], "daytime");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
    tcp::resolver::iterator end;
    tcp::socket socket(io_service);
    asio::error error = asio::error::host_not_found;
    while (error && endpoint_iterator != end)
    {
      socket.close();
      socket.connect(*endpoint_iterator++, asio::assign_error(error));
    }
    if (error)
      throw error;
    for (;;)
    {
      boost::array<char, 128> buf;
      asio::error error;
      size_t len = socket.read_some(
          asio::buffer(buf), asio::assign_error(error));
      if (error == asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw error; // Some other error.
      std::cout.write(buf.data(), len);
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }
  return 0;
}

3.8. TCP同步時(shí)間服務(wù)器

#include <ctime>
#include <iostream>
#include <string>
#include <asio.hpp>
using asio::ip::tcp;

我們先定義一個(gè)函數(shù)返回當(dāng)前的時(shí)間的string形式.這個(gè)函數(shù)會(huì)在我們所有的時(shí)間服務(wù)器示例上被使用.

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
  {
    asio::io_service io_service;

新建一個(gè)asio::ip::tcp::acceptor對(duì)象來(lái)監(jiān)聽(tīng)新的連接.我們監(jiān)聽(tīng)TCP端口13,IP版本為V4

    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));

這是一個(gè)iterative server,也就是說(shuō)同一時(shí)間只能處理一個(gè)連接.建立一個(gè)socket來(lái)表示一個(gè)和客戶(hù)端的連接, 然后等待客戶(hù)端的連接.

    for (;;)
    {
      tcp::socket socket(io_service);
      acceptor.accept(socket);

當(dāng)客戶(hù)端訪(fǎng)問(wèn)服務(wù)器時(shí),我們獲取當(dāng)前時(shí)間,然后返回它.

      std::string message = make_daytime_string();
      asio::write(socket, asio::buffer(message),
          asio::transfer_all(), asio::ignore_error());
    }
  }

最后處理異常

  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }
  return 0;
}

運(yùn)行示例:運(yùn)行服務(wù)器,然后運(yùn)行上一節(jié)的客戶(hù)端,在windowsXP的cmd窗口下

輸入:client.exe 127.0.0.1

輸出:Mon Oct 23 09:44:48 2006

完整的代碼:

#include <ctime>
#include <iostream>
#include <string>
#include <asio.hpp>
using asio::ip::tcp;
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
  {
    asio::io_service io_service;
    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
    for (;;)
    {
      tcp::socket socket(io_service);
      acceptor.accept(socket);
      std::string message = make_daytime_string();
      asio::write(socket, asio::buffer(message),
          asio::transfer_all(), asio::ignore_error());
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }
  return 0;
}

posted on 2006-12-04 12:25 大龍 閱讀(2683) 評(píng)論(2)  編輯 收藏 引用

評(píng)論

# re: BOOST 絕對(duì)實(shí)用手冊(cè)(來(lái)自網(wǎng)絡(luò)) 2007-12-28 18:41 Kouga

很不錯(cuò)的例程,受教了!  回復(fù)  更多評(píng)論   

# re: BOOST 絕對(duì)實(shí)用手冊(cè)(來(lái)自網(wǎng)絡(luò))[未登錄](méi) 2009-02-17 14:13 zz

法克,這不就是boost文檔上的東西么!
http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/tutorial.html  回復(fù)  更多評(píng)論   


只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   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>
            欧美一二三区精品| 欧美综合国产| 99国产精品久久久久久久久久| 亚洲国产成人精品女人久久久| 免费观看30秒视频久久| 久久视频在线视频| 免费成人小视频| 亚洲第一区色| 亚洲日本成人在线观看| 亚洲国产日本| 一区二区三区四区精品| 亚洲一区二区三区中文字幕| 亚洲在线一区二区| 欧美在线观看一区二区| 久久一区亚洲| 欧美激情片在线观看| 欧美日韩一区免费| 国产欧美一区二区精品性色| 韩国三级在线一区| 在线观看亚洲视频| 亚洲久久一区二区| 亚洲在线观看免费| 亚洲欧美视频在线| 久久这里只有| 亚洲人体1000| 亚洲午夜在线视频| 久久久精品性| 欧美欧美午夜aⅴ在线观看| 国产精品久久久久毛片软件| 国产亚洲精品自拍| 亚洲片国产一区一级在线观看| 一区二区三区回区在观看免费视频| 亚洲影视在线播放| 久久久久久九九九九| 亚洲国产成人精品视频| 亚洲一区二区三区视频| 久久精品1区| 欧美日韩国产三区| 国产色产综合产在线视频| 在线观看成人小视频| 亚洲一区二区3| 久久久久一区二区三区| 91久久国产综合久久| 亚洲女同在线| 欧美h视频在线| 国产伦一区二区三区色一情| 91久久夜色精品国产九色| 亚洲女人天堂av| 你懂的亚洲视频| 亚洲无限乱码一二三四麻| 久久九九电影| 国产精品福利久久久| 亚洲国产99| 欧美一区二区三区免费视| 亚洲第一视频网站| 欧美在线不卡| 欧美三级第一页| 亚洲国产一区二区精品专区| 亚洲欧美视频一区| 91久久在线| 久久久av网站| 国产精品一区二区黑丝| 夜夜嗨av色一区二区不卡| 免费在线亚洲欧美| 亚洲永久在线| 欧美日韩免费观看中文| 亚洲国产精品一区二区第四页av | 亚洲欧洲在线播放| 性色一区二区三区| 欧美性事免费在线观看| 亚洲黄色在线| 久久综合给合久久狠狠色| 亚洲免费网站| 欧美日韩一区二| 亚洲国产网站| 免播放器亚洲| 久久精品成人一区二区三区蜜臀| 欧美性猛交xxxx乱大交退制版| 最新成人av网站| 免费久久99精品国产自| 香蕉国产精品偷在线观看不卡| 欧美日韩精品一区二区三区四区 | 亚洲精品乱码久久久久久日本蜜臀| 欧美在线视屏| 国产乱码精品1区2区3区| 亚洲午夜羞羞片| 亚洲免费av电影| 欧美精品色综合| 亚洲精品专区| 亚洲国产精品激情在线观看| 久久久女女女女999久久| 激情欧美一区二区三区| 久久成人综合网| 亚洲免费人成在线视频观看| 国产精品人成在线观看免费| 亚洲免费中文| 亚洲性图久久| 国产精品视频大全| 亚洲欧美日韩在线播放| 亚洲一区二区三区四区中文 | 欧美日韩在线综合| 一本大道久久a久久精品综合| 亚洲黄色av| 欧美精品在线免费观看| 一区二区av在线| 日韩亚洲一区二区| 欧美性片在线观看| 性18欧美另类| 久久www成人_看片免费不卡| 国产一区二区三区四区三区四| 久久久久国产精品一区| 久久久久青草大香线综合精品| 一区二区三区自拍| 欧美激情视频一区二区三区免费 | 日韩午夜激情| 国产精品久久97| 欧美在线一二三区| 欧美在线视频不卡| 亚洲福利免费| 亚洲精品在线免费| 国产精品久久精品日日| 欧美在线视频二区| 久久婷婷久久| 一区二区三区国产精华| 亚洲免费网址| 伊人成年综合电影网| 亚洲高清影视| 国产精品久久国产精品99gif| 午夜一区二区三视频在线观看 | 亚洲精品一区二区在线| 国产精品xvideos88| 久久高清一区| 免费日韩精品中文字幕视频在线| 一个色综合导航| 午夜国产精品影院在线观看| 亚洲大胆女人| 99精品视频一区| 韩国三级电影久久久久久| 亚洲国产成人porn| 国产精品五区| 欧美高清日韩| 欧美无砖砖区免费| 麻豆精品传媒视频| 欧美日韩在线视频首页| 久久久久女教师免费一区| 欧美大片一区二区三区| 欧美一区二区三区久久精品茉莉花| 久久一区免费| 午夜精品视频网站| 六月婷婷一区| 欧美永久精品| 欧美日韩国产成人在线91| 久久国产精品久久久| 欧美伦理在线观看| 久久婷婷影院| 国产精品久久毛片a| 免费在线成人| 国产精品视频导航| 亚洲激情一区二区| 国内精品久久久久影院优| 亚洲美女在线视频| 亚洲电影在线观看| 亚洲自拍偷拍色片视频| 亚洲最新色图| 久久久欧美精品sm网站| 午夜免费电影一区在线观看| 欧美激情91| 男女精品网站| 国产日韩欧美精品一区| 夜夜嗨网站十八久久 | 欧美+日本+国产+在线a∨观看| 国产精品二区影院| 亚洲精美视频| 亚洲国产合集| 欧美一区二区在线免费观看| 亚洲天堂偷拍| 欧美成人在线免费观看| 麻豆9191精品国产| 国产日韩精品一区| 一本久久a久久免费精品不卡| 亚洲经典在线| 久久先锋资源| 久久婷婷蜜乳一本欲蜜臀| 国产精品一区二区久久久久| 夜夜嗨网站十八久久 | 亚洲激情视频| 亚洲国产精品高清久久久| 久久国产精品久久精品国产| 香蕉精品999视频一区二区| 欧美日韩免费看| 亚洲人久久久| 亚洲精品中文字幕在线| 欧美+亚洲+精品+三区| 免费在线观看精品| 精品51国产黑色丝袜高跟鞋| 欧美亚洲网站| 久久国产高清| 国产视频一区欧美| 欧美一级网站| 久久久国产精品一区|