Boost logo

Boost :

Subject: Re: [boost] [asio] Randomly having problems reading from the socket
From: Torben Wiggerich (torben.wiggerich_at_[hidden])
Date: 2011-05-05 09:21:42


Am 05.05.2011 08:15, schrieb 肖锋:
> On Mon, May 2, 2011 at 8:28 PM, Torben Wiggerich
> <torben.wiggerich_at_[hidden]>wrote:
>
>> Hi all,
>>
>> I have some problems using the boost.asio.read for a TCP-socket. So far it
>> works fine, but it randomly stops the transfer for at least 5 seconds.
>> To resolve the error I used a simple synchronous server and client from the
>> boost examples and modified it too send a larger amount of data. (307400
>> Bytes; this size comes from the program I'm trying to get run). On the
>> client side I also changed the reading from the socket to read the complete
>> data with boost.asio.read and transfer_all as argument.
>>
>> Both, server and client, measure the time for sending and reading to the
>> socket and will give an error if it needs more than 1 second.
>>
>> Here is the sourcecode for the server:
>>
>> #include<iostream>
>> #include<string>
>> #include<boost/asio.hpp>
>> #include<boost/date_time/posix_time/posix_time.hpp>
>>
>> using boost::asio::ip::tcp;
>> using namespace boost::posix_time;
>>
>> int main()
>> {
>> try
>> {
>> boost::asio::io_service io_service;
>>
>> tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 8002));
>>
>> tcp::socket socket(io_service);
>> acceptor.accept(socket);
>>
>> unsigned char * array = new unsigned char[307400];
>>
>> for (;;)
>> {
>> boost::system::error_code ignored_error;
>>
>> ptime startTime = microsec_clock::universal_time();
>>
>> boost::asio::write(socket, boost::asio::buffer( array, 307400 ),
>> boost::asio::transfer_all(), ignored_error);
>>
>> time_duration neededTime = microsec_clock::universal_time() -
>> startTime;
>>
>> if ( neededTime.total_milliseconds()> 1000 ) {
>> std::cout<< "ERROR needed too much time: "<<
>> neededTime.total_milliseconds()<< "ms"<< std::endl;
>> }
>> }
>> }
>> catch (std::exception& e)
>> {
>> std::cerr<< e.what()<< std::endl;
>> }
>>
>> return 0;
>> }
>>
>>
>> My client sourcecode:
>>
>> #include<iostream>
>> #include<boost/array.hpp>
>> #include<boost/asio.hpp>
>> #include<boost/date_time/posix_time/posix_time.hpp>
>>
>> using boost::asio::ip::tcp;
>> using namespace boost::posix_time;
>>
>> int main(int argc, char* argv[])
>> {
>> try
>> {
>> if (argc != 2)
>> {
>> std::cerr<< "Usage: client<host>"<< std::endl;
>> return 1;
>> }
>>
>> boost::asio::io_service io_service;
>> boost::asio::ip::tcp::endpoint endpoint(
>> boost::asio::ip::address::from_string( argv[1] ), 8002 );
>>
>> tcp::socket socket(io_service);
>> boost::system::error_code error = boost::asio::error::host_not_found;
>> socket.connect( endpoint, error );
>> if (error)
>> throw boost::system::system_error(error);
>>
>> int counter = 0;
>> unsigned char* array = new unsigned char[307400];
>> for (;;)
>> {
>> boost::system::error_code error;
>>
>> ptime startTime = microsec_clock::universal_time();
>> size_t len = boost::asio::read( socket,
>> boost::asio::buffer( array, 307400 ),
>> boost::asio::transfer_all(),
>> error );
>> time_duration neededTime = microsec_clock::universal_time() -
>> startTime;
>>
>> if ( error ) {
>> printf( "body read error %s\n", error.message().c_str() );
>> }
>>
>> if ( counter % 5000 == 0 ) {
>> std::cout<< "passed: "<< counter<< "\r";
>> }
>> ++counter;
>>
>> if ( neededTime.total_milliseconds()> 1000 ) {
>> std::cout<< "ERROR needed too much time: "<<
>> neededTime.total_milliseconds()<< "ms"<< std::endl;
>> }
>> }
>> }
>> catch (std::exception& e)
>> {
>> std::cerr<< e.what()<< std::endl;
>> }
>>
>> return 0;
>> }
>>
>> To reproduce the error just let it run for a while. It can take up to 30
>> minutes or even more...
>> After a while it will produce the line: "ERROR needed too much time:
>> 5007ms". Maybe with slightly different times, but all around 5 seconds.
>> I tested it on different machines with Windows Vista and Seven.
>>
>> So am I'm doing sth. wrong? Is the reading from the socket correct?
>>
>> I'm using boost version 1.44.
>>
> Your server program exits immediately on my Win7 machine with an exception
> thrown from the line "tcp::acceptor acceptor(...)".
> It gives the same error message as yours after I change the port from 8002
> to 8003.
> And the program works fine after I change the port to 18002.
> So I guess the problem is the port you are using. Maybe it's reserved for
> some other usage and you can not use these ports.
>
> Regards,
> Feng
>
Hi Feng,

I tried many different ports already. But I couldn't find a port which
works... On your suggested port 18002 it also stops on my machine. It is
just a matter of time until it fails.
I tested this on two machines. The first stoped in two hours 6 times and
the other only once.

Kind regards
Torben


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk