- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在探索 boost asio 产品
客户端发送一个 1 字节的 header ,指示后面的字节长度。
相关服务器代码:
enum {max_length=1};
void handle_read(const boost::system::error_code & error, const size_t & bytes_transferred){
if (! error){
++ctr;
std::string inc_data_str(this->inc_data.begin(),this->inc_data.end());
std::cout<<"received string: "<<inc_data_str<<" with size "<<inc_data_str.size()
<<" bytes_transferred: "<<bytes_transferred<<" ctr: "<<ctr<<std::endl;
int size_inc_next = boost::lexical_cast<int>(inc_data_str);
int offset = 0;
//std::cout<<"incoming integer of size "<<size_inc_next<<" processed from string: "<<inc_data_str<<std::endl;
std::vector<char> next_inc_data(size_inc_next+offset);
boost::asio::read(this->socket,boost::asio::buffer(next_inc_data),boost::asio::transfer_exactly(size_inc_next+offset));
std::string int_recvd(next_inc_data.begin(),next_inc_data.begin()+size_inc_next);
//std::cout<<boost::posix_time::microsec_clock::local_time()<<std::endl;
//std::cout<<"received integer: "<<int_recvd<<" from string "<<int_recvd<<" of size "<<int_recvd.size()<<std::endl;
this->process_connection();
} // ! error
} // handle_read
void process_connection(){
boost::asio::async_read(this->socket,boost::asio::buffer(this->inc_data),boost::asio::transfer_exactly(max_length),
boost::bind(&Connection::handle_read,shared_from_this(),boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
相关客户端代码:
void on_write(const boost::system::error_code & error_code){
if (! error_code){
std::string transfer_data("15");
std::vector<char> v_td(transfer_data.begin(),transfer_data.end());
++ctr;
for (std::vector<char>::iterator iter = v_td.begin(); iter != v_td.end(); ++iter) std::cout<<*iter;
std::cout<<" ctr: "<<ctr;
std::endl(std::cout);
boost::asio::async_write(this->socket,boost::asio::buffer(v_td),boost::asio::transfer_exactly(2),
boost::bind(&Client::on_write,shared_from_this(),
boost::asio::placeholders::error));
}
}
服务器进程的预期示例打印输出:
received string: 1 with size 1 bytes_transferred: 1 ctr: 159685
客户端进程的预期示例打印输出:
15 ctr: 356293
这样的预期输出会产生一段时间,但是在 356293 次客户端迭代之后(这个 ctr 数字从重复的进程试验中肉眼看是不确定的),服务器中断并出现以下错误:
received string: with size 1 bytes_transferred: 1 ctr: 159686
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >'
what(): bad lexical cast: source type value could not be interpreted as target
中止(核心转储)
请注意,收到的字符串是“空白”。有时它也会因替代消息而中断:
received string: X with size 1 bytes_transferred: 1 ctr: 159686
这是怎么回事,为什么以及我该如何解决?
strace 之后的进一步编辑:
客户端跟踪:
sendmsg(6, {msg_name(0)=NULL, msg_iov(1)=[{"15", 2}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 2
epoll_wait(4, {}, 128, 0) = 0
write(1, "15 ctr: 204441\n", 1515 ctr: 204441) = 15
sendmsg(6, {msg_name(0)=NULL, msg_iov(1)=[{"15", 2}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 2
epoll_wait(4, {}, 128, 0) = 0
write(1, "15 ctr: 204442\n", 1515 ctr: 204442) = 15
sendmsg(6, {msg_name(0)=NULL, msg_iov(1)=[{"15", 2}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = -1 EAGAIN (Resource temporarily \
unavailable)
epoll_wait(4, {{EPOLLOUT, {u32=167539936, u64=167539936}}}, 128, -1) = 1
sendmsg(6, {msg_name(0)=NULL, msg_iov(1)=[{"\0\0", 2}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 2
write(1, "15 ctr: 204443\n", 1515 ctr: 204443) = 15
sendmsg(6, {msg_name(0)=NULL, msg_iov(1)=[{"15", 2}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 2
epoll_wait(4, {}, 128, 0) = 0
write(1, "15 ctr: 204444\n", 1515 ctr: 204444) = 15
sendmsg(6, {msg_name(0)=NULL, msg_iov(1)=[{"15", 2}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 2
epoll_wait(4, {}, 128, 0) = 0
write(1, "15 ctr: 204445\n", 1515 ctr: 204445) = 15
服务器跟踪:
write(1, "received string: 1 with size 1 b"..., 64received string: 1 with size 1 bytes_transferred: 1 ctr: 204441) = 64
write(1, "incoming integer of size 1 proce"..., 52incoming integer of size 1 processed from string: 1) = 52
recvmsg(7, {msg_name(0)=NULL, msg_iov(1)=[{"5", 1}], msg_controllen=0, msg_flags=0},0) = 1
write(1, "received integer: 5 from string "..., 44received integer: 5 from string 5 of size 1) = 44
recvmsg(7, {msg_name(0)=NULL, msg_iov(1)=[{"1", 1}], msg_controllen=0, msg_flags=0},0) = 1
epoll_wait(4, {}, 128, 0) = 0
write(1, "received string: 1 with size 1 b"..., 64received string: 1 with size 1 bytes_transferred: 1 ctr: 204442) = 64
write(1, "incoming integer of size 1 proce"..., 52incoming integer of size 1 processed from string: 1) = 52
recvmsg(7, {msg_name(0)=NULL, msg_iov(1)=[{"5", 1}], msg_controllen=0, msg_flags=0}, 0) = 1
write(1, "received integer: 5 from string "..., 44received integer: 5 from string 5 of size 1) = 44
recvmsg(7, {msg_name(0)=NULL, msg_iov(1)=[{"\0", 1}], msg_controllen=0, msg_flags=0}, 0) = 1
epoll_wait(4, {}, 128, 0) = 0
write(1, "received string: \0 with size 1 b"..., 64received string: ^@ with size 1 bytes_transferred: 1 ctr: 204443) = 64
futex(0xb76640fc, FUTEX_WAKE_PRIVATE, 2147483647) = 0
write(1, "inc_data_str\n", 13inc_data_str) = 13
对于客户端进程,错误的“\0\0”发送之前的 epoll_wait 与其他 epoll_wait 调用不同(u32=...., u64=....)...不知道这意味着什么
总结一下令人困惑的部分,strace 表示正在传输空值,但下一行的 strace 表示对标准输出的写系统调用,文字为“15”,这意味着这就是 transfer_data vector 中的内容
重新编辑:
最后我插入了一个
boost::this_thread::sleep(boost::posix_time::microseconds(200));
就在客户端 on_write 函数中的 write 语句之前。
有了这个,没有遇到任何问题。那么 asio 对象会出现什么样的并发问题呢?是 socket 吗?
最佳答案
您的客户端因缓冲区生命周期而损坏
void
on_write(
const boost::system::error_code& error_code
)
{
if ( !error_code ) {
std::string transfer_data("15");
std::vector<char> v_td(transfer_data.begin(), transfer_data.end());
// ^
// \------ goes out of scope before async_write() returns
boost::asio::async_write(
this->socket,
boost::asio::buffer(v_td),
boost::asio::transfer_exactly(2),
boost::bind(
&Client::on_write,
shared_from_this(),
boost::asio::placeholders::error
)
);
}
}
您需要确保给async_write()
的缓冲区在调用完成处理程序之前保持有效:
buffers One or more buffers containing the data to be written. Although the buffers object may be copied as necessary, ownership of the underlying memory blocks is retained by the caller, which must guarantee that they remain valid until the handler is called. handler
关于c++ - boost asio tcp - 套接字写入数据不同于缓冲区中的数据 - 某处可能存在线程不安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228509/
这个问题已经有答案了: What is the difference between a variable, object, and reference? [duplicate] (5 个回答) 已关
我正在使用以下代码来学习java套接字编程。它的作用是,client.java 程序从用户那里获取一个号码并将其发送到 sever.java。然后服务器将其乘以2并发回给客户端。在我的客户端程序中,它
我编写了一个自己开发的串行端口类,为了简单起见,我使用了阻塞/同步/非重叠。我浏览了所有 MSDN 文档,这对我来说很困难。 我在从端口打开、传输或接收字节方面没有任何问题。所有操作都是同步并且没有线
//Not finished -- disregard function evaluate() { var cdate = new Date(); var cday = cdate.getDa
我已经尝试过了,但它有效 例如: x= 523.897 y= x[0:"."] print y 我只想打印 523。如何让 Python 抓取字符串直到某个字母或数字? 最佳答案 行 y = x[0:
我想移动拐 Angular 处的方框(从左上角开始水平移动 Angular 落到右上角然后你去到右下角。 function myMove() { var elem = document.getEl
如何让侧边栏停止在第二个侧边栏部分而不是顶部? fiddle http://jsfiddle.net/EvAdP/2/ HTML I'm the header
我刚刚在大学开始我的第二门编程类(class),我们的第一个作业相当简单,旨在基本上检查我们的环境并检查我们是否知道如何通过类(class)网站提交作业。 当我运行我们提供的代码时,它卡在应该提示用户
我目前正忙于我的这个信息学元素(构建一个示例交友网站)。问题是我在把所有东西都放在正确的地方时遇到了一些麻烦。一切都很顺利,直到我的邮箱出现在错误的位置(但是,Dreamweaver 会按照我的意
我想,和你一样hover a GIF shot on Dribbble , 当光标位于元素高度顶部之后/50% 处时显示带有信息的 div。 测试示例 我做了这个,这是有效的,但有点棘手......特
我有一个下拉菜单,并且我已将 mouseenter 设置为选项。因此,如果鼠标位于触发器之外,菜单应该关闭。我正在使用 jQuery 1.8.0。这可能是 CSS 问题吗? 这是我初始化插件的代码。
我正在尝试在 Visual Basic for Applications 中编写一个 hello world 应用程序,即修改 Excel 工作表中的单元格。这是: Sub hello() D
我的应用程序使用 JSF 2.1 和 PrimeFaces。最近,由于一些线程卡住,观察到 CPU 利用率非常高。所有卡住线程的卡住线程转储都指向 javax.faces.component.UICo
在列出 aws cognito 用户时,我的 Node js 应用程序遇到问题。 仅当我有超过 60 个 Cognito 用户时才会出现此问题。 Reference of API 下面是我的代码片段。
我是 ubuntu 用户..我在 php 中有一个执行 python 文件的命令..python 文件设置为可执行文件..所以,我的 php 命令是:- shell_exec("try.py");
我正在尝试将剪贴板内容写入文件,但由于某种原因程序卡住了。 FILE *fp; fp = fopen("tmp.code","w"); fprintf(fp,getclip()); /*writes*
当用户向下滚动时,我使用此代码使侧边栏固定在某个 div 处。问题是我必须手动输入一个阈值数字,这并不总是理想的,因为该部分的位置可能会更改或在各种浏览器和系统之间不一致。我想知道是否有一种方法可以在
我有一个字符串数组,例如 first_page = {{"U","M","Y","Q","I","A","L","D","P"、"F"、"E"、"G"、"T"、"Z"、"V"、"W"、"H"、"O"、
我能否在页面上的特定 px 位置放置一个元素(例如图像),然后让文本围绕它流动? 必要时使用 JS/jquery。 我确实看到了这个Have text flow around an object th
第一次在这里提问。 我有两个简单的 Javascript 函数,1. 生成一个随机字母,2. 在每个单元格中使用单个字母填充 10x10 表格。创建表的主要函数是通过带有 onclick 的简单 HT
我是一名优秀的程序员,十分优秀!