boost asio 中具有异步功能的不良字符
我在 boost::asio 中遇到数据传输问题。 使用以下代码:
Server.cpp
void Server::accept(void)
{
Network::ptr connection = Network::create(this->my_acceptor.get_io_service());
this->my_acceptor.async_accept(connection->getSocket(), bind(&Server::endCmd, this, connection, placeholders::error));
}
void Server::endCmd(Network::ptr connection, const boost::system::error_code& error)
{
if (!error)
{
connection->Swrite("BIENVENUE");
this->accept();
}
}
Network.cpp
void Network::Sread(void)
{
async_read(this->socket, buffer(this->rbuffer), bind(&Network::endRead, shared_from_this(), placeholders::error));
}
void Network::endRead(const error_code& error)
{
if (!error)
{
this->rcv_msg = this->rbuffer.c_array();
std::cout << this->rcv_msg << std::endl;
this->Sread();
}
}
void Network::Swrite(std::string msg)
{
this->msg = msg;
async_write(this->socket, buffer(this->msg, (int)this->msg.size()), bind(&Network::endWrite, shared_from_this(), placeholders::error));
}
void Network::endWrite(const error_code &error)
{
if (!error)
{
this->Sread();
}
}
tcp::socket& Network::getSocket(void)
{
return (this->socket);
}
Network::ptr Network::create(io_service &ios)
{
return (ptr(new Network(ios)));
}
当我使用 telnet 向服务器发送类似“Hello world”的字符串时,他写入以下内容:
谁能告诉我为什么服务器会写入许多未知字符?
i have a problem with data transfert in boost::asio.
with the following code :
Server.cpp
void Server::accept(void)
{
Network::ptr connection = Network::create(this->my_acceptor.get_io_service());
this->my_acceptor.async_accept(connection->getSocket(), bind(&Server::endCmd, this, connection, placeholders::error));
}
void Server::endCmd(Network::ptr connection, const boost::system::error_code& error)
{
if (!error)
{
connection->Swrite("BIENVENUE");
this->accept();
}
}
Network.cpp
void Network::Sread(void)
{
async_read(this->socket, buffer(this->rbuffer), bind(&Network::endRead, shared_from_this(), placeholders::error));
}
void Network::endRead(const error_code& error)
{
if (!error)
{
this->rcv_msg = this->rbuffer.c_array();
std::cout << this->rcv_msg << std::endl;
this->Sread();
}
}
void Network::Swrite(std::string msg)
{
this->msg = msg;
async_write(this->socket, buffer(this->msg, (int)this->msg.size()), bind(&Network::endWrite, shared_from_this(), placeholders::error));
}
void Network::endWrite(const error_code &error)
{
if (!error)
{
this->Sread();
}
}
tcp::socket& Network::getSocket(void)
{
return (this->socket);
}
Network::ptr Network::create(io_service &ios)
{
return (ptr(new Network(ios)));
}
When i send a string like "Hello world" to the server with telnet, he write the following content :
Who can tell me why the server is writting many unknow characters ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信你的 readHandler (Network::endRead()) 缺少一个参数。无法确定从流中读取了多少字节。我的回调通常如下所示(但我调用 async_read_some()):
然后我会在打印之前以 null 终止从流中读取的字符串。
编辑:
我对 async_read_some() 的调用如下所示:
I believe your readHandler (Network::endRead()) is missing an argument. There is no way to determine how many bytes were read from the stream. My callback usually looks like this (but I call async_read_some()):
Then I would null terminate the string I've read from the stream before printing.
Edit:
My call to async_read_some() looks like this:
看起来 rbuffer.c_array() 不是一个以 null 结尾的字符串,而只是一个字符数组。将字符数组打印到 cout 假定数组以 null 终止,在这种情况下,这会导致数组末尾之后的内存包含在输出中。
在尝试打印之前,您应该从数据创建一个
std::string
:(假设
rbuffer
有一个size()
方法或等效方法)It looks like the
rbuffer.c_array()
isn't a null terminated string but just a array of characters. Printing a character array tocout
assumes that the array is null terminated, which in this case causes the memory after the end of the array being included in the output.You should create a
std::string
from the data before trying to print it:(Assuming
rbuffer
has asize()
method or something equivalent)