如何使用Boost.Asio的C ++? [英] How to use Boost.Asio c++?

查看:176
本文介绍了如何使用Boost.Asio的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尝试使用库使用插座Boost.Asio的C ++在多个平台上。
我下载了最新版本在这里:

I would try to use the library to use socket Boost.Asio c++ on multiple platforms. I downloaded the latest version here:

http://sourceforge.net/projects/boost/files/boost /1.46.1/

但现在我该怎么在我的code使用?
我编译它?包括刚刚够吗?
你能告诉我的步骤?

but now what do I use in my code? I have compile it? include just enough? Can you tell me the steps?

推荐答案

如何使用取决于你想要做什么, - 。)

How you use it depends on what you want to do, ;-).

该文档在这里找到:

<一个href=\"http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio.html\">http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio.html

您会发现很多的应该适合你的需要的例子。

You will find lots of examples that should suite your needs.

有关的建筑,你应该注意的是,库的依赖关系取决于您是否在Windows或Linux上运行。看到这里

For building, you should note that the library dependancies depend upon whether you are running on windows or linux. See here

<一个href=\"http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/using.html\">http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/using.html

在特定的:

随着MSVC或Borland的C ++,你可能希望
  添加-DBOOST_DATE_TIME_NO_LIB和
  -DBOOST_REGEX_NO_LIB到您的项目设置来禁用的autolinking
  的Boost.Date_Time和Boost.Regex
  分别图书馆。此外,
  您可以选择建立这些
  库和链接到他们。

With MSVC or Borland C++ you may want to add -DBOOST_DATE_TIME_NO_LIB and -DBOOST_REGEX_NO_LIB to your project settings to disable autolinking of the Boost.Date_Time and Boost.Regex libraries respectively. Alternatively, you may choose to build these libraries and link to them

如果你不希望依赖关系到其他Boost库,那么你可以使用非升压(我想其他相同的ASIO)库从这里:的 http://think-async.com/

If you don't want the dependancies to the other boost libraries then you can use the non-boost (i think otherwise identical asio) library from here: http://think-async.com/

有关其他文件来源见SO这个问题:为升压最好的文档:ASIO

For sources of other documentation see this question on SO: Best documentation for Boost:asio ?

作为一个例子,打开一个串口你可能会这样写:

As an example, to open a serial port you might write something like this:

/** Manage serial connections.*/
class serial_manager
{

  boost::asio::io_service m_io_service;
  std::string m_name;
  const unsigned int m_baud_rate;
  const enum flow_control::type m_flow_control;
  const enum parity::type m_parity;
  const enum stop_bits::type m_stop_bits;
  const unsigned int m_char_size;
  boost::asio::serial_port  m_SerialPort;
  boost::system::error_code m_error;
public:

  /** A constructor.
   *  @param name The dvice name, for example "COM1" (windows, or "/dev/ttyS0" (linux).
   *  @param baud_rate The baud rate. 
   *  @param flow_control The flow control. Acceptable values are flow_control::none, flow_control::software, flow_control::hardware.
   *  @param parity The parity of the connection. Acceptable values are parity::none, parity::even, parity::odd.
   *  @param stop_bits The number of stop bits. Acceptable values are stop_bits::one, stop_bits::one_point_five, stop::bits::two
   *  @param char_size The number of characters in connection.
   */
  serial_manager(const std::string& name, 
         const unsigned int& baud_rate  = 19200,
         const enum flow_control::type&   flow_control  = flow_control::none,
         const enum parity::type&         parity        = parity::none,
         const enum stop_bits::type&      stop_bits     = stop_bits::one,
         const unsigned int& char_size  = 8
         ) 
;
  void open();
};

void
serial_manager::open() {
  if (!m_SerialPort.is_open()) 
    {
      m_SerialPort.open(m_name, m_error);

     if (m_error == boost::system::errc::no_such_file_or_directory ) 
     {  //for example you tried to open "COM1" on a linux machine.
        //... handle the error somehow
     }

      m_SerialPort.set_option(boost::asio::serial_port::baud_rate(m_baud_rate));
      m_SerialPort.set_option(boost::asio::serial_port::flow_control(m_flow_control));
      m_SerialPort.set_option(boost::asio::serial_port::parity(m_parity));
      m_SerialPort.set_option(boost::asio::serial_port::stop_bits(m_stop_bits));
      m_SerialPort.set_option(boost::asio::serial_port::character_size(m_char_size));


    }
 }

这篇关于如何使用Boost.Asio的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆