惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Scott Helme
Scott Helme
S
Securelist
美团技术团队
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
AI
AI
L
Lohrmann on Cybersecurity
S
Security Affairs
Cloudbric
Cloudbric
SecWiki News
SecWiki News
爱范儿
爱范儿
雷峰网
雷峰网
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
I
InfoQ
S
Secure Thoughts
AWS News Blog
AWS News Blog
A
About on SuperTechFans
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
The Last Watchdog
The Last Watchdog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
P
Palo Alto Networks Blog
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
Latest news
Latest news
I
Intezer
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
D
Docker

博客园 - hcfalan

Mybatis分页功能 安聊服务端Netty的应用 安聊系统1.0发布 VC中发送电子邮件 自绘窗口边框和标题栏 Windows下生产者-消费者问题的解法 Javascript 时间日期转换 Java Threading - Consumer&Producer MFC CSocket的跨线程问题 用于主题检测的临时日志(54b8134e-5e4a-46fc-95af-c75ccf06d66e - 3bfe001a-32de-4114-a6b4-4005b770f6d7) 网络电话 VC 剪贴板操作 The 1st Boost Serial Demo - hcfalan MS Proxy FTP Sample 《斯坦福大学开放课程: 编程方法学》 VC2005下编译log4cpp的修正方法 - hcfalan - 博客园 适合孩子的书籍 一个最经典的线程类(转) 应用MFC开发高级应用程序(转)
Boost socket 同步编程示例(服务端,客户端)
hcfalan · 2011-02-23 · via 博客园 - hcfalan

服务端程序代码:

 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 }