gpt4 book ai didi

c++ - 为什么 ASIO async write 网络第二次发送数据失败?

转载 作者:行者123 更新时间:2023-11-28 06:36:48 27 4
gpt4 key购买 nike

尝试通过一个连接发送多个请求:

int count = boost::asio::write(socket, request); // this will result in normal count2

int count2 = boost::asio::write(socket, request); // this one gives zero

然而 cont2 等于零意味着 id 不发送数据.. Ho 修复它

添加更多代码以获得更好的 View :

boost::asio::io_service io_service;

boost::asio::streambuf lastResponse;

// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
char *host = "somewebsie.coom";
char *requestPath = "/somequery";
tcp::resolver::query query(host, "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

// Try each endpoint until we successfully establish a connection.
tcp::socket socket(io_service);
boost::asio::socket_base::keep_alive option(true);
boost::asio::socket_base::non_blocking_io option2(true);
boost::asio::socket_base::non_blocking_io command(true);


double startTime,endTime;
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "GET "<< requestPath <<" HTTP/1.1\r\nHost: "<<host <<":80\r\n\r\n";

boost::asio::connect(socket, endpoint_iterator);
socket.set_option(option);
socket.io_control(command);

startTime = getRealTime( );

// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.


int count2 = boost::asio::write(socket, request);
count2 = boost::asio::write(socket, request);
bool isopen = socket.is_open(); // return true...

最佳答案

为什么要用刚刚清空的流调用 write?!尝试写入零字节不是一个好主意。

您没有尝试发送多个请求。您的代码中只有一个请求,您已经发送了它。

关于c++ - 为什么 ASIO async write 网络第二次发送数据失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26645208/

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