I have a problem while closing the client app, when server is closed.
I have my read thread and a class which implements tcp connection.
Code is below:
This is in my main file - main.cpp
void thread_handling()
{
if(g_serverconnected)
{
boost::thread workerThread1((boost::bind(&TCP::RcvData_sck,g_Client));
workerThread1.join();
}
}
TCP.cpp
int TCP::RcvData_sck()
{
boost::system::error_code e;
int length;
try
{
for (;;)
{
length=m_Socket.read_some(boost::asio::buffer(m_RecieveBuffer),e);
if(e)
throw boost::system::system_error(e);
std::vector<uint8_t>buffer_read_notify;
for(int i=0;i<length;i++)
{
buffer_read_notify.push_back(m_RecieveBuffer[i]);
}
if(length>0)
{
memset(m_RecieveBuffer,m_BufLen,0);
}
buffer_read_notify.resize(0);
m_IOService.run();
if(e==boost::asio::error::eof)
{
std::cout<<"connection closed by router";
g_serverconnected = false;
m_IOService.stop();
m_Socket.close()
break;
}
m_IOService.reset();
}
}
catch (std::exception& e)
{
g_serverconnected = false;
m_IOService.stop();
m_Socket.close()
return 0;
}
}
But I am stuck in a scenario where server ends the connection.
I receive exception : "An existing connection was forcibly closed by the remote host".
My threading part is in different file which is out of scope of class.
I want is to join thread when ever I get the exception.
How do I achieve this?
Any help or tip to guide will be much appreciated.
What I have tried:
I have tried keeping try-catch block within my for loop of read and break whenever exception is found, but it didn't work.
Also, I tried return 0 for the function when exception arrives based on which I will join the thread to main process but not sure how to evaluate it in thread_handling() function.If some one can explain that also,then it would be really helpful.