write_some: 管道 C++ 损坏
write_some: Broken pipe C++
我对 boost::asio
有疑问。我正在尝试创建基本聊天,但遇到 write/write_some: Broken pipe
错误。这是代码
客户端
此处留言为空vector<char> message
,data = "Client"
createMessage(message, data)
将数据放入消息
void Client::connect()
{
int port = 9999;
boost::asio::io_context ioc;
boost::system::error_code ec;
boost::asio::ip::tcp::endpoint address(boost::asio::ip::address::from_string("127.0.0.1"), port);
boost::asio::ip::tcp::socket socket(ioc);
socket.connect(address, ec);
if(ec)
{
std::cerr<<ec<<std::endl;
}
for(;;)
{
createMessage(message, data);
// VARIANT 1
boost::asio::write(socket, boost::asio::buffer(message.data(), message.size()));
// VARIANT 2
//socket.write_some(boost::asio::buffer(message));
}
socket.shutdown(boost::asio::socket_base::shutdown_both);
}
服务器端
我的服务器 运行 在本地主机上,端口为 9999。
void Server::start()
{
boost::system::error_code ec;
std::vector<char> buffer;
buffer.reserve(2 * 1024);
boost::asio::ip::tcp::endpoint address(boost::asio::ip::tcp::v4(), port);
boost::asio::ip::tcp::acceptor acc(ioc, address);
for(;;)
{
boost::asio::ip::tcp::socket socket(ioc);
acc.accept(socket);
if(ec)
{
std::cerr<<ec<<std::endl;
return;
}
socket.read_some(boost::asio::buffer(buffer.data(), buffer.size()));
for(auto& i : buffer)
{
std::cout<<i<<std::flush;
}
}
}
你能给出任何线索吗?
boost::asio::buffer(buffer.data(), buffer.size())
可能是
boost::asio::buffer(buffer)
它不起作用,因为 buffer.size()
总是 为零。您应该使用 asio::dynamic_buffer
或 resize(n)
而不是 reserve(n)
演示
我即兴创作了一个更完整的例子Live On Coliru
#include <boost/asio.hpp>
#include <iostream>
#include <iomanip>
static boost::asio::io_context ioc;
static constexpr uint16_t port = 9999;
using boost::asio::ip::tcp;
using boost::system::error_code;
void createMessage(std::string& message, int data) {
message = std::to_string(data);
while (data--)
message += message;
}
void Client_connect(std::string message, int data) {
error_code ec;
tcp::endpoint address({}, port);
tcp::socket socket(ioc);
socket.connect(address, ec);
for (;data-- && !ec;) {
createMessage(message, data);
boost::asio::write(socket, boost::asio::buffer(message), ec);
}
std::cerr << ec.message() << std::flush;
socket.shutdown(boost::asio::socket_base::shutdown_both);
}
void Server_start() {
tcp::acceptor acc(ioc, {{}, port});
for (;;) {
tcp::socket socket(ioc);
error_code ec;
acc.accept(socket, ec);
for (std::string buffer; !ec;) {
buffer.resize(100); // max 100 byte reads
auto n = socket.read_some(boost::asio::buffer(buffer), ec);
buffer.resize(n);
std::cout << std::quoted(buffer) << std::flush;
}
std::cerr << ec.message() << std::endl;
}
}
int main(int argc, char**) {
if (argc>1)
Client_connect("", 7);
else
Server_start();
}
打印例如客户:
Success
服务器:
"6666666666666666666666666666666666666666666666666666666666666666555555555555555555555555555555554444"
"444444444444333333332222110"
""
End of file
我对 boost::asio
有疑问。我正在尝试创建基本聊天,但遇到 write/write_some: Broken pipe
错误。这是代码
客户端
此处留言为空vector<char> message
,data = "Client"
createMessage(message, data)
将数据放入消息
void Client::connect()
{
int port = 9999;
boost::asio::io_context ioc;
boost::system::error_code ec;
boost::asio::ip::tcp::endpoint address(boost::asio::ip::address::from_string("127.0.0.1"), port);
boost::asio::ip::tcp::socket socket(ioc);
socket.connect(address, ec);
if(ec)
{
std::cerr<<ec<<std::endl;
}
for(;;)
{
createMessage(message, data);
// VARIANT 1
boost::asio::write(socket, boost::asio::buffer(message.data(), message.size()));
// VARIANT 2
//socket.write_some(boost::asio::buffer(message));
}
socket.shutdown(boost::asio::socket_base::shutdown_both);
}
服务器端
我的服务器 运行 在本地主机上,端口为 9999。
void Server::start()
{
boost::system::error_code ec;
std::vector<char> buffer;
buffer.reserve(2 * 1024);
boost::asio::ip::tcp::endpoint address(boost::asio::ip::tcp::v4(), port);
boost::asio::ip::tcp::acceptor acc(ioc, address);
for(;;)
{
boost::asio::ip::tcp::socket socket(ioc);
acc.accept(socket);
if(ec)
{
std::cerr<<ec<<std::endl;
return;
}
socket.read_some(boost::asio::buffer(buffer.data(), buffer.size()));
for(auto& i : buffer)
{
std::cout<<i<<std::flush;
}
}
}
你能给出任何线索吗?
boost::asio::buffer(buffer.data(), buffer.size())
可能是
boost::asio::buffer(buffer)
它不起作用,因为 buffer.size()
总是 为零。您应该使用 asio::dynamic_buffer
或 resize(n)
而不是 reserve(n)
演示
我即兴创作了一个更完整的例子Live On Coliru
#include <boost/asio.hpp>
#include <iostream>
#include <iomanip>
static boost::asio::io_context ioc;
static constexpr uint16_t port = 9999;
using boost::asio::ip::tcp;
using boost::system::error_code;
void createMessage(std::string& message, int data) {
message = std::to_string(data);
while (data--)
message += message;
}
void Client_connect(std::string message, int data) {
error_code ec;
tcp::endpoint address({}, port);
tcp::socket socket(ioc);
socket.connect(address, ec);
for (;data-- && !ec;) {
createMessage(message, data);
boost::asio::write(socket, boost::asio::buffer(message), ec);
}
std::cerr << ec.message() << std::flush;
socket.shutdown(boost::asio::socket_base::shutdown_both);
}
void Server_start() {
tcp::acceptor acc(ioc, {{}, port});
for (;;) {
tcp::socket socket(ioc);
error_code ec;
acc.accept(socket, ec);
for (std::string buffer; !ec;) {
buffer.resize(100); // max 100 byte reads
auto n = socket.read_some(boost::asio::buffer(buffer), ec);
buffer.resize(n);
std::cout << std::quoted(buffer) << std::flush;
}
std::cerr << ec.message() << std::endl;
}
}
int main(int argc, char**) {
if (argc>1)
Client_connect("", 7);
else
Server_start();
}
打印例如客户:
Success
服务器:
"6666666666666666666666666666666666666666666666666666666666666666555555555555555555555555555555554444"
"444444444444333333332222110"
""
End of file