zoukankan      html  css  js  c++  java
  • Boost.asio入门心得: 编译和链接的注意事项

      最近准备跟乐搞个IM. 网络这个坑终究还是准备想跳了, 于是拿来Boost.asio小试牛刀. 折腾了几天, 也遇到了不少问题.  参考资料: Boost官方文档. 

      Boost中大部分库包含hpp文件即可直接使用, 少部分需要预先编译. asio这个库依赖了几个需要编译的库: system, regex, date_time, serialization, thread. 所以, 要使用asio, 首先要编译这几个库. 如官方文档所说(http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/using.html#boost_asio.using.building_boost_libraries):

      You may build the subset of Boost libraries required to use Boost.Asio and its examples by running the following command from the root of the Boost download package:

    bjam --with-system --with-thread --with-date_time --with-regex --with-serialization stage

      具体bjam的编译选项可以参考bjam的相关文档. 或者直接使用bjam --help也可以看到bjam的各种参数的说明. 这里不再赘述. 

      编译好之后, 照着tutorial, 拉下来两份代码, 分别是Daytime.1和Daytime.2

    http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/tutorial/tutdaytime1.html

    http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/tutorial/tutdaytime2.html

      文档里面对代码几乎每行都有注释了, 而且结构简单也不难理解. 但是在编译的过程中遇到点麻烦.使用gcc(mingw)分别在linux下和windows下进行编译. 

      lwindows下的makefile如下: 

    
    

    all:client.exe server.exe

    
    

    client.exe:client.cpp
    g++ client.cpp -o client.exe \
      -I D:\workplace\tools\boost_1_49_0 \
      -L D:\workplace\tools\boost_1_49_0\stage\lib \
      -static -lpthread -lboost_thread-mgw44-mt-1_49 \
      -lboost_system-mgw44-mt-1_49 \
      -lboost_date_time-mgw44-mt-1_49 \
      -lboost_regex-mgw44-mt-1_49 \
      -lwsock32 \
      -lws2_32


    server.exe:server.cpp
      g++ server.cpp -o server.exe \
      -I D:\workplace\tools\boost_1_49_0 \
      -L D:\workplace\tools\boost_1_49_0\stage\lib \
      -static -lpthread -lboost_thread-mgw44-mt-1_49 \
      -lboost_system-mgw44-mt-1_49 \
      -lboost_date_time-mgw44-mt-1_49 \
      -lboost_regex-mgw44-mt-1_49 \
      -lwsock32 \
      -lws2_32

     

      除了需要链接boost_thread, boost_system, boost_date_time, boost_regex之外, 还需要链接pthread, wsock32和ws2_32. 在linux下, 就不需要wsock32和ws2_32了. 

      另外关于代码中的一点说明: 

      在代码server.cpp中, 

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

      tutorial中的端口号设置为13. 实际上貌似5000一下的端口号都是系统使用了的. 将这个值设置为一个大于5000的值. 

      在代码client.cpp中, 

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

      这一行query构造函数的两个参数, 分别代表的主机的ip地址和端口号(或服务名称). 嘛, 看来看去没看出来server那边咋知道自己叫做 "daytime" 的. 于是将第二个参数改成server.cpp中设定的端口号的值. 

      编译成功之后, 首先运行server.exe, 然后运行client.exe, 在命令行参数中输入server的ip地址. 如果是本机为server的话可以直接使用 "localhost" 或者 "127.0.0.1" , 然后会打印出client从server中获取的时间. 

      这个看似简单的过程其实还是不知不觉的折腾了许久. 终于搞定了. 下一步继续研究下asio的各种基本用法, 研究下example里面的内容. 

      

  • 相关阅读:
    SQL Functions
    wse 3.0
    mvc2 在 .net 4 下的ValidateInput(false) 无效
    FF3.0 不可以post空
    也谈.NET MVC 2 + ExtJS的部署问题
    ExtJs懒人笔记(2) ExtJs页面布局
    关于算法—— 一维字符串数组之间组合问题的C#实现
    (转)在ASP.NET 中实现单点登录(利用Cache, 将用户信息保存在服务器缓存中)
    XML中配置正则表达式的写法
    .NET MVC 下实现消息推送
  • 原文地址:https://www.cnblogs.com/HGtz2222/p/2614473.html
Copyright © 2011-2022 走看看