[Boost-users] asio ip::tcp::socket::read(), always returns end of file

2,607 views
Skip to first unread message

Lloyd

unread,
Jun 6, 2011, 4:10:10 AM6/6/11
to Boost Users
Hi,

I have an asio::ip::tcp::socket. Whenever I try to read from it, it
throws an exception "end of file". But it is possible to write to the
socket at any time! This is a client side application, it connects to
the server using asio::async_connect(). The server is sending back the
data, I checked it with wireshark. I tried using the both async_read()
and read(), in both cases it fails with the same error. But both
async_write(), and write() works perfectly. What could be the reason?

This is how the read call implemented

...
....
boost::asio::streambuf b;
boost::asio::read(sock,b); //This line causes exception

...

Thanks,
Lloyd
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Martin Dyring-Andersen

unread,
Jun 6, 2011, 4:51:56 AM6/6/11
to Boost Users
Hi Lloyd,

> I have an asio::ip::tcp::socket. Whenever I try to read from it, it
> throws an exception "end of file". But it is possible to write to the
> socket at any time! This is a client side application, it connects to
> the server using asio::async_connect(). The server is sending back the
> data, I checked it with wireshark. I tried using the both async_read()
> and read(), in both cases it fails with the same error. But both
> async_write(), and write() works perfectly. What could be the reason?

You don't provide a lot of information about the protocol, but the behavior you are seeing might be the server shutting down the servers' sending side of the connection, like

sock.shutdown(boost::asio::ip::tcp::socket::shutdown_send, error);

which still will leave the client able to write() data to the socket. The client should be able to read the data sent from the server before the shutdown() though.

Just an idea.

Best regards,
Martin Dyring-Andersen

Lloyd

unread,
Jun 6, 2011, 5:01:17 AM6/6/11
to boost...@lists.boost.org
Thanks Martin. The server sends back the data, I have checked it with
wireshark. I am able to sniff and see the data.

This program is not using any complex protocol, it is simple "tcp"
based text protocol. first of all am not able to read anything from
the server.

One more thing I would like to add is the async_connect() is called
from a thread, the io_service is also inside the thread. there are
multiple treads like this. But I have tried removing the thread, that
too does not mak any difference. I have also tried blocking
connect(),write(), and read(), now also the read fails!

If this post is not clear, I am ready to post my code...

Thanks
Lloyd

Lloyd

unread,
Jun 6, 2011, 6:32:46 AM6/6/11
to boost...@lists.boost.org
This code reproduces the problem. I am not able to find why exception
is thrown. I have removed error checking to make the code to read
easier.

Thanks,
Lloyd

class Connection:public boost::enable_shared_from_this<Connection>
{
public:
Connection(boost::asio::io_service& ios):sock(ios)
{
}
void Connect()
{
sock.connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string("172.16.29.12"),13));
boost::asio::write(sock,boost::asio::buffer("RUN 1\r\n",7));
try
{


boost::asio::streambuf b;
boost::asio::read(sock,b);
}

catch(exception& e)
{
cout<<e.what()<<endl; //"This exception is thrown always-> "End of file"
}
}
private:
boost::asio::ip::tcp::socket sock;

};

class MainClass
{
public:
void Connect()
{
boost::thread_group tg;
tg.create_thread(boost::bind(&MainClass::Thread,this));
tg.join_all();
}

void Thread()
{
boost::asio::io_service ios;
boost::shared_ptr<Connection> asc=boost::shared_ptr<Connection>(new
Connection(ios));
asc->Connect();
ios.run();
}
};

int main()
{
MainClass mc;
mc.Connect();
return 0;

Igor R

unread,
Jun 6, 2011, 7:10:39 AM6/6/11
to boost...@lists.boost.org
>                        boost::asio::read(sock,b);
>                }
>                catch(exception& e)
>                {
>                        cout<<e.what()<<endl; //"This exception is thrown always-> "End of file"
>                }

So your peer sends some data and closes the socket.
asio::read() reads this data, and then throws the exception. What
behavior would you expect?

Please note that this is the *only* way this overload can complete.
Otherwise, it would just block forever.
http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/reference/read/overload4.html

Lloyd

unread,
Jun 6, 2011, 8:54:20 AM6/6/11
to boost...@lists.boost.org
Thanks Igor. So I was debugging a problem that doesn't exist :(

Igor R

unread,
Jun 6, 2011, 9:08:06 AM6/6/11
to boost...@lists.boost.org
> Thanks Igor. So I was debugging a problem that doesn't exist :(

Well, it's worth reading the documentation :).
Note that most of asio functions have throwing and non-throwing overloads.


HTH,

Igor'.

Reply all
Reply to author
Forward
0 new messages