gpt4 book ai didi

c++ - boost::asio::ip::tcp::socket 不读取任何内容

转载 作者:可可西里 更新时间:2023-11-01 02:37:10 25 4
gpt4 key购买 nike

这实际上是我在套接字编程方面的第一个程序,而不是复制粘贴教程代码并从中获得乐趣。无论如何它不起作用。我想我已经仔细阅读了文档,但也许还不够。我怀疑我在 read_message() 中使用了 socket::read_some(),因为我有问题的程序在读取部分停止了。我认为我使用它的方式应该没问题,因为 socket::read_some() 的文档指出“函数调用将阻塞,直到一个或多个字节的数据被成功读取,或者直到一个发生错误。”下面是我的代码。任何帮助表示赞赏。

void read_message(std::string& message, boost::asio::ip::tcp::socket& socket)
{
std::stringstream message_stream;
while (true)
{
std::array<char, 128> buffer;
boost::system::error_code error;
size_t len = socket.read_some(boost::asio::buffer(buffer), error);
if (error == boost::asio::error::eof)
{
break;
}
message_stream.write(buffer.data(), len);
}
message = message_stream.str();
}

服务器.cpp

int main()
{
while (true)
{
boost::asio::io_service io_service;
boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(
boost::asio::ip::tcp::v4(), 9999));
boost::asio::ip::tcp::socket socket(io_service);
acceptor.accept(socket);
std::cout << "connected with client, waiting for a message\n";
std::string message;
read_message(message, socket);
boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
std::cout << "message received from " << endpoint.address().to_string() <<
':' << endpoint.port() << '\n' << message << '\n';
boost::asio::write(socket, boost::asio::buffer(message));
if (message.compare("quit") == 0)
{
break;
}
}
return EXIT_SUCCESS;
}

客户端.cpp

int main()
{
while (true)
{
std::cout << "your message to be sent: ";
std::string message;
std::getline(std::cin, message);
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query("localhost", "9999");
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
boost::asio::ip::tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
std::cout << "connected to server\n";
boost::asio::write(socket, boost::asio::buffer(message));
read_message(message, socket);
std::cout << "message received from server: " << message << '\n';
if (message.compare("quit") == 0)
{
break;
}
}
return EXIT_SUCCESS;
}

最佳答案

问题出在 read_message 函数中:您一直读取到 eof,但只有当套接字关闭时您才会收到 eof

也许您打算在遇到某些分隔符时从 read_message 返回,例如。 '\n'?然后您可以手动检查缓冲区,或使用 read_until功能。

使用 read_until 函数,read_message 将如下所示:

void read_message(std::string& message, boost::asio::ip::tcp::socket& socket)
{
std::stringstream message_stream;
boost::asio::streambuf buffer;
boost::system::error_code error;
size_t len = read_until(socket, buffer, '\n', error);
if (len)
{
message_stream.write(boost::asio::buffer_cast<const char *>(buffer.data()), len);
message = message_stream.str();
}
}

请记住在您从客户端发送的消息中附加“\n”:

//...
std::cout << "connected to server\n";
boost::asio::write(socket, boost::asio::buffer(message + '\n'));
read_message(message, socket);
//...

关于c++ - boost::asio::ip::tcp::socket 不读取任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26957299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com