I have a problem in socket programming with boost asio.
The flow of my program is below:
The client use async_write to send data to server, then it will use async_read to receive the data from server. All of the handler of async operations is use_future, and the timeout = 2 seconds.
The problem is:
The processing time of server is about 10 ms. Both the server and the client is in the same computer, so the client should receive the data while the server sends them out. However, I measured the latency of async_read and I found that some of them are about 10 ms and others are about 30 ~ 40 ms. Usually, they are interleaved.
How do I measured:
auto start_time = std::chrono::steady_clock::now();
auto read_result = async_read(..., use_future);
auto read_future_status = read_result.wait_for(timeout);
auto end_time = std::chrono::steady_clock::now();
I have tried 2 solutions :
- Allocate larger space to boost::asio::streambuf. I allocated with 4096 Bytes and my data size never exceed 1500 Bytes, but it doesn't work.
- Use
socket_.set_option(ip::tcp::no_delay(true));to turn off the Nagle's Algorithm and Delay Ack. Also, it cannot work.
I have no idea with the problem. Could anyone can help me?? Please...
Updates: Here is parts of my source code
Below is the code of send request to server:
auto send_result = async_write(input_socket, out_buffer, use_future);
auto send_status = send_result.wait_for(std::chrono::seconds(timeout));
if(send_status == std::future_status::timeout)
{
LOG4CPLUS_ERROR_FMT(logger_, "send %s future error : (%d).", action, send_status);
}
out_buffer.consume(request.length());
Below is the code of receive data from server.
auto read_buffer_result = async_read(output_socket, in_buffer, transfer_exactly(sizeof(Header)), use_future);
auto read_status = read_buffer_result.wait_for(std::chrono::seconds(timeout));
if(read_status == std::future_status::timeout)
{
LOG4CPLUS_ERROR_FMT(logger_, "read %s header future error : (%d).", action, read_status);
}
size_t byte_transferred = read_buffer_result.get();
in_buffer.consume(byte_transferred);
main()function in it, which exhibits the problem. Something We can simply cut and paste into an ide to check. - Richard Hodges