boot asio 非阻塞同步编程---非阻塞的accept和receive。

boot asio 非阻塞同步编程---非阻塞的accept和receive。

客户端编程:

[cpp] view plain copy
 
  1. #include<boost/timer.hpp>   
  2. #include <iostream>  
  3. #include <boost/asio.hpp>  
  4. #include <stdlib.h>  
  5.   
  6. using namespace boost::asio;  
  7. using namespace std;  
  8.   
  9. #define RECEIVE_BUF_SIZE 100  
  10.   
  11. #define RECEIVE_BYTE_NUM 30  
  12.   
  13. int readMaxBytesInTime(ip::tcp::socket & socket,char * strBuf,int nMaxBytes,int nMilSec)  
  14. {  
  15.     boost::timer t;  
  16.     int nTotalRec = 0;  
  17.     int nLeftBytes = nMaxBytes - nTotalRec;  
  18.     while(1)  
  19.     {  
  20.         boost::system::error_code ec;  
  21.         char buf[RECEIVE_BUF_SIZE];  
  22.   
  23.         int nWantBytes = 0;  
  24.         if(nLeftBytes < RECEIVE_BUF_SIZE)  
  25.         {  
  26.             nWantBytes = nLeftBytes;  
  27.         }  
  28.         else  
  29.         {  
  30.             nWantBytes = RECEIVE_BUF_SIZE;  
  31.         }  
  32.   
  33.         size_t len=socket.read_some(buffer(buf,nWantBytes), ec);  
  34.         if(len>0)  
  35.         {  
  36.             memcpy(strBuf + nTotalRec,buf,len);  
  37.             nTotalRec += len;  
  38.             nLeftBytes -= len;  
  39.   
  40.             if(nLeftBytes <= 0)  
  41.                 break;  
  42.             else  
  43.                 continue;  
  44.         }  
  45.         else  
  46.         {  
  47.             if(t.elapsed()*1000 < nMilSec)  
  48.             {  
  49.                 Sleep(0);  
  50.                 continue;  
  51.             }  
  52.             else  
  53.                 break;  
  54.         }  
  55.     }  
  56.     return nTotalRec;  
  57. }  
  58.   
  59. int main(int argc, char* argv[])  
  60. {  
  61.   
  62.     // 所有asio类都需要io_service对象  
  63.     io_service iosev;  
  64.     // socket对象  
  65.     ip::tcp::socket socket(iosev);  
  66.     socket.open(boost::asio::ip::tcp::v4());  
  67.     socket.io_control(boost::asio::ip::tcp::socket::non_blocking_io(true));  
  68.     // 连接端点,这里使用了本机连接,可以修改IP地址测试远程连接  
  69.     ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 1000);  
  70.     // 连接服务器  
  71.     boost::system::error_code ec;  
  72.     boost::timer t;  
  73.     socket.connect(ep,ec);  
  74.     cout<< t.elapsed()<<"s"<<endl;   
  75.     system("PAUSE");  
  76.   
  77.     // 如果出错,打印出错信息  
  78.     if(ec)  
  79.     {  
  80.         std::cout << boost::system::system_error(ec).what() << std::endl;  
  81.         return -1;  
  82.     }  
  83.     // 接收数据  
  84.     char buf[RECEIVE_BYTE_NUM];  
  85.     int len = readMaxBytesInTime(socket,buf,RECEIVE_BYTE_NUM,1000);  
  86.   
  87.     std::cout<<"接收字节数:"<<len<<std::endl;  
  88.     std::cout.write(buf, len);  
  89.   
  90.     system("PAUSE");  
  91.   
  92.     return 0;  
  93. }  

服务器端编程:

[cpp] view plain copy
 
  1. #include <iostream>  
  2. #include <boost/asio.hpp>  
  3.   
  4. #include <stdlib.h>  
  5.   
  6. int main(int argc, char* argv[])  
  7. {  
  8.     using namespace boost::asio;  
  9.     // 所有asio类都需要io_service对象  
  10.     io_service iosev;  
  11.     ip::tcp::acceptor acceptor(iosev);  
  12.     acceptor.open(boost::asio::ip::tcp::v4());  
  13.     acceptor.io_control(boost::asio::ip::tcp::socket::non_blocking_io(true));  
  14.     // 连接端点,这里使用了本机连接,可以修改IP地址测试远程连接  
  15.     ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 1000);  
  16.     acceptor.bind(ep);  
  17.     acceptor.listen();  
  18.     for(;;)  
  19.     {  
  20.         boost::system::error_code ec;  
  21.         // socket对象  
  22.         ip::tcp::socket socket(iosev);  
  23.   
  24.         // 等待直到客户端连接进来  
  25.         while (1)  
  26.         {  
  27.             acceptor.accept(socket,ec);  
  28.             if(ec)  
  29.             {  
  30.                 std::cout <<  
  31.                     boost::system::system_error(ec).what() << std::endl;  
  32.                 Sleep(10);  
  33.             }  
  34.             else  
  35.                 break;  
  36.         }  
  37.           
  38.         system("PAUSE");  
  39.   
  40.         // 显示连接进来的客户端  
  41.         std::cout << socket.remote_endpoint().address() << std::endl;  
  42.   
  43.         // 向客户端发送hello world!  
  44.         char * str = "hello world!hello world!";  
  45.         socket.write_some(buffer(str,20), ec);  
  46.   
  47.         // 如果出错,打印出错信息  
  48.         if(ec)  
  49.         {  
  50.             std::cout <<  
  51.                 boost::system::system_error(ec).what() << std::endl;  
  52.             break;  
  53.         }  
  54.         // 与当前客户交互完成后循环继续等待下一客户连接  
  55.     }  
  56.     return 0;  
  57. }  
原文地址:https://www.cnblogs.com/lidabo/p/8317236.html