安装卸载更新boost.asio以及跑通demo全过程

开始学习网络编程,以安装boost 1.61.0版本为例。服务器信息如下:

[root@sklse UNP]# uname -a
Linux sklse 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

首先看下服务器是否安装了boost:

[root@sklse UNP]# rpm -qa boost*
boost-date-time-1.41.0-28.el6.x86_64
boost-program-options-1.41.0-28.el6.x86_64
boost-devel-1.41.0-28.el6.x86_64
boost-filesystem-1.41.0-28.el6.x86_64
boost-system-1.41.0-28.el6.x86_64
boost-serialization-1.41.0-28.el6.x86_64
boost-regex-1.41.0-28.el6.x86_64
boost-iostreams-1.41.0-28.el6.x86_64
boost-thread-1.41.0-28.el6.x86_64
boost-signals-1.41.0-28.el6.x86_64
boost-wave-1.41.0-28.el6.x86_64
boost-math-1.41.0-25.el6.x86_64
boost-1.41.0-28.el6.x86_64
boost-python-1.41.0-28.el6.x86_64
boost-graph-1.41.0-28.el6.x86_64
boost-test-1.41.0-28.el6.x86_64

发现已经安装了1.41.0版本的boost,尝试直接更新到最新版本:

[root@sklse UNP]# yum check-update boost*
Loaded plugins: aliases, changelog, downloadonly, fastestmirror, kabi, presto, refresh-packagekit, security,
              : tmprepo, verify, versionlock
Loading support for CentOS kernel ABI
Loading mirror speeds from cached hostfile
 * base: mirrors.njupt.edu.cn
 * extras: mirrors.njupt.edu.cn
 * updates: mirrors.njupt.edu.cn
 * webtatic: sp.repo.webtatic.com

boost-math.x86_64                                      1.41.0-28.el6

发现库里面并没有更新的boost,考虑更新库,从网上也下载了rpm的更新库,但是发现还是没有最新的boost,最新的版本还是1.41.0的。然后就考虑卸载掉旧的版本,自己重新安装新的版本。

rm -rf /usr/lib64/libboost*
rm -rf /usr/include/boost

从网上下载boost 1.61.0版本:http://www.boost.org/users/download/

解压缩得到boost-1.61.0文件夹,进入boost-1.61.0目录,执行:

./bootstrap.sh
./b2
./b2 install --prefix=/usr/local/boost

执行./boostrap.sh会编译boost.Build,生成b2构建命令,然后执行 b2 install就开始编译生成链接库,执行完成后会在/usr/local/boost目录中生成 include 和 lib 两个文件夹。

为了g++能找到boost头文件和库文件,需要将他们加入环境变量。

对于头文件而言,直接打开~/.bashrc, 加入 export PATH=${PATH}:/usr/local/boost/include

对于库文件而言,需要使用ldconfig命令告诉操作系统库有更新,可以直接执行ldconfig /usr/local/boost/lib,或者直接打开/etc/ld.so.conf, 在最后加入一行/usr/local/boost/lib,这样操作系统才能找到新添加的库文件,只在环境变量中加入路径也是找不到的。

在boost文档中找到一个daytime的测试例子,以root用户登录:
新建client.c文件,文件内容:(http://www.boost.org/doc/libs/1_64_0/doc/html/boost_asio/tutorial/tutdaytime1/src.html)

#include 
#include 
#include 

using boost::asio::ip::tcp;

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

    boost::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::socket socket(io_service);
    boost::asio::connect(socket, endpoint_iterator);

    for (;;)
    {
      boost::array, 128> buf;
      boost::system::error_code error;

      size_t len = socket.read_some(boost::asio::buffer(buf), error);

      if (error == boost::asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw boost::system::system_error(error); // Some other error.

      std::cout.write(buf.data(), len);
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

然后新建一个server.c文件,内容为:
(http://www.boost.org/doc/libs/1_64_0/doc/html/boost_asio/tutorial/tutdaytime2/src.html)

#include 
#include 
#include 
#include 

using boost::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
  {
    boost::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();

      boost::system::error_code ignored_error;
      boost::asio::write(socket, boost::asio::buffer(message), ignored_error);
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

编译:

g++ client.c -o client -lboost_system -lboost_thread
g++ server.c -o server -lboost_system -lboost_thread

打开两个终端,一个终端先启动server程序监听,然后另外一个终端执行client发送请求:

./server
./client localhost

执行成功,client会返回当前日期时间:

[root@sklse UNP]# ./client localhost
Tue Jun 13 16:09:55 2017

可以使用ss -ta 或者netstat -at来查看网络监听状态:

[root@sklse UNP]# ss -ta | grep daytime
LISTEN     0      128                     *:daytime                  *:* 
[root@sklse UNP]# netstat -at | grep daytime
tcp        0      0 *:daytime                   *:*                         LISTEN 

参考:http://blog.csdn.net/ariessurfer/article/details/7984001

你可能感兴趣的:(network,boost)