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

推荐订阅源

博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
T
Tailwind CSS Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
N
News and Events Feed by Topic
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
K
Kaspersky official blog
WordPress大学
WordPress大学
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 叶小钗
W
WeLiveSecurity
Jina AI
Jina AI
The Cloudflare Blog
Project Zero
Project Zero
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
L
LangChain Blog
Forbes - Security
Forbes - Security
PCI Perspectives
PCI Perspectives
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
博客园 - 【当耐特】
H
Heimdal Security Blog
A
About on SuperTechFans
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
L
LINUX DO - 最新话题
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
Intezer
Martin Fowler
Martin Fowler
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint

博客园 - 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 }