zoukankan      html  css  js  c++  java
  • boost asio

      摘自: https://mmoaay.gitbooks.io/boost-asio-cpp-network-programming-chinese/content/Chapter2.html

      asio封装了操作系统的select、kqueue、poll/epoll I/O等机制,实现异步IO模型。在同步模式下,程序发起一个IO操作,向io_service提交请求,io_service把操作转交给操作系统,同步地等待。当IO操作完成时,操作系统通知io_service,然后io_service再把结果发回给程序,完成整个同步流程。在异步模式下,程序除了要发起IO操作,还要定义一个用于回调的完成处理函数。io_service同样把IO操作转交给操作系统执行,但它不同步等待,而是立即返回。调用io_service的run()成员函数可以等待异步操作完成,当异步操作完成时io_service从操作系统获取结果,在调用handler执行后续逻辑;

      用于TCP通信的typedef类型,包括端点类endpoint、套接字类socket、流类iostream,以及接收器acceptor、解析器resolver等。 

    class tcp
    {
    public:
    /// The type of a TCP endpoint.
      typedef basic_endpoint<tcp> endpoint;   // 包含IP地址和通信用的端口号

    /*****************************************************************************************/

      endpoint():这是默认构造函数,某些时候可以用来创建UDP/ICMP socket; 

      endpoint(protocol, port):这个方法通常用来创建可以接受新连接的服务器端socket;

      endpoint(addr, port):这个方法创建了一个连接到某个地址和端口的端点;

    /*****************************************************************************************/


    /// The TCP socket type.
      typedef basic_stream_socket<tcp> socket;
    /// The TCP acceptor type.
      typedef basic_socket_acceptor<tcp> acceptor;
    /// The TCP resolver type.
      typedef basic_resolver<tcp> resolver;
    /// The TCP iostream type.
      typedef basic_socket_iostream<tcp> iostream;
    /// ......
    };

      acceptor对应Socket API的accept()函数,用于服务器端。acceptor可以像传统socket API一样使用,open()打开端口,bind()绑定再用listen()侦听端口,但更方便的是使用它的构造函数,传入endpoint直接完成这三个动作;

    未完, 待续,,

  • 相关阅读:
    正则表达式
    HDU 2066 多源最短路
    UVA 11039 模拟
    Concrete Mathematics Chapter 1 Warmups
    List differences between JAVA and C++
    uva 11107Life Forms
    poj 1509 Glass Beads
    poj 3581
    网络流建图
    图论算法----网络流
  • 原文地址:https://www.cnblogs.com/kilen/p/9515328.html
Copyright © 2011-2022 走看看