2014-10-30 85 views
0

试图通过一个连接来发送多个请求:为什么ASIO同步写入newtork无法第二次发送数据?

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不发送数据..何来修复它

添加更多的代码获得更好的视野:

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... 

回答

1

你为什么打电话给write带一个你刚刚清空的流?试图写零字节不是一个好主意。

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

+0

真的!经过四年的C#很难回到C++ :) – 2014-10-30 12:20:17

相关问题