Boost socket 同步编程示例(服务端,客户端)

服务端程序代码:

 

 1  //  BoostServer.cpp : 定义控制台应用程序的入口点。
 2  //
 3 
 4 #include  " stdafx.h "
 5 #include <iostream> 
 6 #include <boost/asio.hpp> 
 7 
 8  using  namespace boost::asio;
 9  #define BLOCK_SIZE 64*1024
10 
11  int main( int argc,  char* argv[])
12 {
13      //  所有asio类都需要io_service对象 
14     io_service iosev; 
15     ip::tcp::acceptor acceptor(iosev,  
16         ip::tcp::endpoint(ip::tcp::v4(),  1000)); 
17      for(;;) 
18     { 
19          //  socket对象 
20         ip::tcp::socket socket(iosev); 
21          //  等待直到客户端连接进来 
22         acceptor.accept(socket); 
23          //  显示连接进来的客户端 
24         std::cout <<  " client from:  " 
25             << socket.remote_endpoint().address() << std::endl; 
26         
27         boost::system::error_code ec;
28 
29          //  从客户端读取数据
30          char buf[BLOCK_SIZE];
31          int len = socket.read_some(buffer(buf), ec);
32          //  或者可以使用read_until读到某个字符为止
33           //  或者可以使用某种判断方式循环读取
34 
35          if (ec)
36         {
37             std::cout <<  
38                 boost::system::system_error(ec).what() << std::endl; 
39              break
40         }
41         std::cout.write(buf, len);
42         std::cout << len << std::endl;
43 
44         Sleep( 1000);
45 
46          //  向客户端发送 
47         len = socket.write_some(buffer(buf, len), ec); 
48          if(ec) 
49         { 
50             std::cout <<  
51                 boost::system::system_error(ec).what() << std::endl; 
52              break
53         } 
54         std::cout <<  " writed  " << len << std::endl;
55          //  与当前客户交互完成后循环继续等待下一客户连接 
56     } 
57 
58 
59      return  0
60 
61 }

 

 

客户端程序代码:

 

 1  //  BoostClient.cpp : 定义控制台应用程序的入口点。
 2  //
 3 
 4 #include  " stdafx.h "
 5 #include <iostream> 
 6 #include <boost/asio.hpp> 
 7 
 8  using  namespace boost::asio; 
 9 
10  #define BLOCK_SIZE 64*1024
11 
12  void fill_buffer( char* s)
13 {
14      for ( int i= 0; i<BLOCK_SIZE; i++)
15     {
16         s[i] =  ' a ';
17     }
18 }
19 
20  int main( int argc,  char* argv[])
21 {
22      //  所有asio类都需要io_service对象 
23     io_service iosev; 
24      //  socket对象 
25     ip::tcp::socket socket(iosev); 
26      //  连接端点,这里使用了本机连接,可以修改IP地址测试远程连接 
27     ip::tcp::endpoint ep(ip::address_v4::from_string( " 127.0.0.1 "),  1000); 
28      //  连接服务器 
29     boost::system::error_code ec; 
30     socket.connect(ep,ec); 
31      //  如果出错,打印出错信息 
32      if(ec) 
33     { 
34         std::cout << boost::system::system_error(ec).what() << std::endl; 
35          return - 1
36     } 
37      //  发送数据
38      char buf[BLOCK_SIZE];
39     fill_buffer(buf);
40     size_t len = socket.write_some(buffer(buf), ec);
41     std::cout <<  " writed  " << len << std::endl;
42 
43      //  接收数据 
44     memset(buf,  0, BLOCK_SIZE);
45     len=socket.read_some(buffer(buf), ec); 
46     std::cout.write(buf, len); 
47     std::cout << len << std::endl;
48 
49      return  0
50 
51 }

 

从演示代码可以得知

  • ASIO的TCP协议通过boost::asio::ip名 空间下的tcp类进行通信。
  • IP地址(address,address_v4,address_v6)、 端口号和协议版本组成一个端点tcp:: endpoint)。用于在服务器端生成tcp::acceptor对 象,并在指定端口上等待连接;或者在客户端连接到指定地址的服务器上。
  • socket是 服务器与客户端通信的桥梁,连接成功后所有的读写都是通过socket对 象实现的,当socket析 构后,连接自动断 开。
  • ASIO读写所用的缓冲区用buffer函 数生成,这个函数生成的是一个ASIO内部使用的缓冲区类,它能把数组、指针(同时指定大 小)、std::vector、std::string、boost::array包装成缓冲区类。
  • ASIO中的函数、类方法都接受一个boost::system::error_code类 型的数据,用于提供出错码。它可以转换成bool测试是否出错,并通过boost::system::system_error类 获得详细的出错信息。另外,也可以不向ASIO的函数或方法提供 boost::system::error_code,这时如果出错的话就会直 接抛出异常,异常类型就是boost::system:: system_error(它是从std::runtime_error继承的)

 转自http://www.cppblog.com/janvy/archive/2010/03/24/110478.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值