Boost Asio未完成对套接字的写入
Boost Asio incomplete write to socket
我正在使用Boost Asio编写一个非常简单的玩具键值存储,但发生了一些非常奇怪的事情。
基于字符串的协议如下:
S <key> <value> // to set a key
G <key> // to get the key value
L // to list all key-value pairs
写入是同步的,使用
boost::asio::write(socket_,boost::asic::buffer(resp,len));
其中socket是一个提升::asio::ip::tcp::socket-使用异步写入,故事显然不会改变。
问题是有时它没有将应该写入的所有字节写入套接字,或者输出以某种方式被破坏。。。
损坏的列表输出示例(在localhost上,使用nc、echo和hexdump):
> echo S a 12 | nc localhost 5000
A
> echo S b 23 | nc localhost 5000
A
> echo L | nc localhost 5000 | hexdump -C
00000000 61 3a 20 31 32 3b 20 62 60 00 00 00 00 00 |a: 12; b`.....|
0000000e
> echo L | nc localhost 5000 | hexdump -C
00000000 61 3a 20 31 32 3b 20 62 3a 20 32 33 3b 20 |a: 12; b: 23; |
0000000e
我使用的是Ubuntu 14.10存储库中的Boost 1.55。遵循为客户端提供服务的函数的代码。
提前感谢您的任何提示!
void ClientSession::handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
if (!error) {
std::string cmd(data_, bytes_transferred);
cmd = trim_str(cmd);
const char* resp = NULL;
int len = 0;
switch(cmd.at(0)) {
case SET: {
std::size_t k_pos = cmd.find(" ") + 1;
std::size_t v_pos = cmd.find(" ", k_pos+1) + 1;
std::string key = trim_str(cmd.substr(k_pos, v_pos-3));
std::string value = trim_str(cmd.substr(v_pos, cmd.length()-1));
cout << "SET key " << key << ", value " << value << "*" <<endl;
kvs->db[key] = std::atoi(value.c_str());
resp = "A";
len = 1;
break;
}
case GET: {
std::size_t k_pos = cmd.find(" ") + 1;
std::string key = trim_str(cmd.substr(k_pos, cmd.length()));
cout << "GET key " << key << "*" << endl;
int value = kvs->db[key];
char str[5];
sprintf(str, "%d", value);
resp = (const char*) str;
len = strlen(resp);
break;
}
case LIST: {
ostringstream os;
for (std::map<string, int>::iterator iter = kvs->db.begin();
iter != kvs->db.end(); ++iter )
os << iter->first << ": " << iter->second << "; ";
cout << "list: " << os.str().c_str() << endl;
resp = os.str().c_str();
len = strlen(resp);
break;
}
case DEL: {
std::size_t k_pos = cmd.find(" ") + 1;
std::string key = trim_str(cmd.substr(k_pos, cmd.length()));
kvs->db.erase(key);
resp = "A";
len = 1;
break;
}
default: {
resp = "NACK.";
len = 5;
}
}
cout << "resp: " << resp << "*" << endl;
cout << "len: " << len << "*" << endl;
std::size_t written = boost::asio::write(socket_,
boost::asio::buffer(resp, len));
cout << "written: " << written << endl;
boost::system::error_code ignored_ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
socket_.close();
} else
delete this;
至少您有未定义的行为,因为case GET
:中有悬挂的resp
指针
{
// ...
char str[5];
resp = (const char*) str; // WHOOOOOOOOOOOPS
len = strlen(resp);
}
case LIST
:下的情况完全相同
{
std::ostringstream os;
// ....
resp = os.str().c_str(); // WHOOOOOOOOOOOPS
}
当您有未定义的行为时,有关程序的所有推理都会停止。
修复这些问题(可能还有更多我没有发现的问题),然后重新测试。在山谷中奔跑。使用静态分析工具。
更新:单线程的固定版本:https://gist.github.com/sehe/69379e17350fb718892f#comment-1428235
试运行输出:
$ for a in S{a..d} $RANDOM Gnonexisting L; do echo "$a -> $(netcat 127.0.0.1 5000 <<< "$a")"; done | nl
1 Sa 15936 -> A
2 Sb 3671 -> A
3 Sc 10550 -> A
4 Sd 7741 -> A
5 Gnonexisting -> 0
6 L -> 1: 1; 2: 2; a: 15936; asdasd: 0; b: 3671; c: 10550; d: 7741; nonexisting: 0;
handle_read
的代码看起来像:
void ClientSession::handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
if (!error) {
std::istringstream request(std::string(data_, bytes_transferred));
boost::asio::streambuf resp;
std::ostream os(&resp);
char cmd_char = 0;
std::string key;
int value;
if (request >> cmd_char) switch(cmd_char) {
case SET:
if (request >> key >> value)
kvs->db[key] = value;
os << "A";
break;
case GET:
if (request >> key)
os << kvs->db[key];
break;
case LIST:
for (auto const& e : kvs->db)
os << e.first << ": " << e.second << "; ";
break;
case DEL:
if (request >> key)
kvs->db.erase(key);
os << "A";
break;
default:
os << "NACK.";
}
cout << "resp: " << &resp << "*" << endl;
cout << "len: " << resp.size() << "*" << endl;
std::size_t written = boost::asio::write(socket_, resp);
cout << "written: " << written << endl;
boost::system::error_code ignored_ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
socket_.close();
} else
delete this;
}
相关文章:
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- 在C ++ ASIO中,如何为HTTP或HTTPS创建相同的套接字对象(接口/抽象问题?
- ASIO 的 TCP 套接字::async_wait() 并在此之后关闭套接字
- 在网络套接字计时器滴答后增加asio短读错误
- Asio如何包含基于BSD套接字API的低级套接字接口
- 如何从 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> 获取本机套接字文件描述器?
- boost::asio-async_read_some示例代码没有读取套接字中的所有数据
- 提升 ASIO TCP 套接字 1.70 不向后兼容
- 为异步发送缓冲区保留内存(提升 asio 套接字)
- C++ 关闭Boost ASIO SSL套接字的最佳方法是什么?
- 尝试关闭 SSL 套接字时使用升压 asio 1.64 的分段错误 (SIGSEGV)
- 使用 boost::asio 获取 UDP 套接字远程地址
- 如何在 boost asio 中检测套接字上的关闭连接,而无需阻塞调用
- 如何使用 boost asio 通过 UDP 套接字发送无符号字符的 std::vector?
- 在 boost::asio 中自定义套接字/关闭系统调用
- 是否可以在使用 Boost::asio 之前从套接字读取后执行async_handshake
- 使用 Boost.Asio 将 UDP 套接字绑定到特定网络接口
- 如何通过超时检查Boost ASIO套接字中的数据
- 委托使用 boost asio TCP/IP 套接字
- 在boost asio tcp套接字上接受多个连接