Boost logo

Boost Users :

Subject: [Boost-users] Too many EOF's using boost::asio
From: ustulation (ustulation_at_[hidden])
Date: 2013-08-30 17:09:29


I have a TCP server and client with following read and write functions:

----------------- Writes ----------------------

void SomeClass::startWrite()
{
   //m_writeBuffer is boost::asio::streambuf..m_writeQueue is
std::deque<std::string>
   m_writeBuffer.sputn(&m_writeQueue.at(0)[0], m_writeQueue.at(0).size());

   //m_objStrandWrite is boost::asio::strand
   boost::asio::async_write(
            getSocket(),
            m_writeBuffer,
            m_objStrandWrite.wrap([this](const boost::system::error_code &e,
std::size_t bytesTxd){writeHandler(e, bytesTxd);})
            );
}

void SomeClass::writeHandler(const boost::system::error_code &refErrorCode,
std::size_t bytesTxd)
{
   if(!m_bIsStopped)
   {
      m_writeQueue.pop_front();

      if(refErrorCode)
      {
         if(refErrorCode != boost::asio::error::operation_aborted)
         {
            std::cerr << "Write Error: " << refErrorCode.message() <<
std::endl;
            stop();
         }
      }
      else
      {
         if(!m_writeQueue.empty())
         {
            startWrite();
         }
      }
   }
}

------------------- Reads -----------------

void SomeClass::startRead()
{
   //m_readBuffer is boost::asio::streambuf..m_objStrandRead is
boost::asio::strand
   boost::asio::async_read_until(
            getSocket(),
            m_readBuffer,
            m_szTermString,
            m_objStrandRead.wrap([this](const boost::system::error_code &e,
std::size_t bytesRxd){readHandler(e, bytesRxd);})
            );
}

void SomeClass::readHandler(const boost::system::error_code &refErrorCode,
std::size_t bytesRxd)
{
   if(!m_bIsStopped)
   {
      if(refErrorCode)
      {
         if(refErrorCode != boost::asio::error::operation_aborted)
         {
            std::cerr << "Read error: " << refErrorCode.message() <<
std::endl;
            stop();
         }
      }
      else
      {
         if(bytesRxd)
         {
            std::string szRxdData;
            szRxdData.resize(bytesRxd);

            m_readBuffer.sgetn(&szRxdData[0], bytesRxd);
         }

         startRead();
      }
   }
}

I get very frequent End Of File (EOF) error on client's read. It is not
always but frequent. I am running a threadpool by asking multiple
std::threads to execute io_service::run() which never quits until
io_service::stop() is called. The data given by the server is almost never
more than 5MB.
As you see in the code, for any error I call stop() which shuts down the
connection gracefully.

<Q> *Is there something wrong I'm doing because I see no reason for EOF
error. Why should the server close/shutdown the connection?* The code does
not do that explicitly. Anyway it is random - out of 10 times maybe 5 are
successful reads and 5 become EOF's and I have to restart my client
executable to connect again in each of those cases. Other sockets don't do
that - eg., I've tested with QTcpSocket of Qt framework and it works all the
time (no randomness).

--
View this message in context: http://boost.2283326.n4.nabble.com/Too-many-EOF-s-using-boost-asio-tp4651173.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net