Click here to Skip to main content
15,888,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
void thread_handling()
{
	/*
	 * Start threading only if server is connected, if not connected Exit Jupiter App
	 */
	if(g_serverconnected)
	{
		/*
		 * Start a thread for reading through TCP socket continuously
		 */
		boost::thread workerThread1((boost::bind(&TCP::RcvData_sck,g_Client));

		/*
		 * #TODO: Shutdown
		 */

		workerThread1.join();
	}
}


TCP.cpp

C#
int TCP::RcvData_sck()
{

	boost::system::error_code e;
	int length;
	try
	{
		for (;;)
		{
			/*
			 *read_some() -  This function is used to read data from the stream socket.
			 * The function call will block until one or more bytes of data has been read successfully, or until an error occurs.
			 * The number of bytes read is returned. Returns 0 if an error occurred.
			 */
			length=m_Socket.read_some(boost::asio::buffer(m_RecieveBuffer),e);
			if(e)
				throw boost::system::system_error(e);
			/*
			 * copy received data to Vector
			 * Dynamic buffer to handle incoming data as length of received data may vary
			 */
			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);
				//process data
			}
			buffer_read_notify.resize(0);
			m_IOService.run();

			if(e==boost::asio::error::eof) //connection closed by router
				{
					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.
Posted
Comments
Jochen Arndt 9-Mar-16 6:47am    
When an exception occurs or the connection is closed, the thread function returns and the thread is no longer active. In such a case calling workerThread1.join() would return immediately.

But calling workerThread1.join() while the thread is still running will block until the thread is terminated. That is obviously not what you want.

I have a problem with understanding what you finally want to achieve. As long as you clean-up upon execptions and closed connections, all should be OK. But you should at least return a value in all cases (there is no return statement at the end).

If your goal is to restart receiving upon specific conditions or you need to reset connection related objects in your main thread, implement some kind of event handling in your main thread that is signalled when the thread terminates. Your main thread can then re-connect and call thread_handling() again or just perform clean-up.
Member 10305620 9-Mar-16 22:41pm    
The problem is when exception occurs my thread is not returning also as a clean up I am closing the socket when exception occurs but my code just gets "stuck" at exception.Thread never joins the main process.I am not sure why is it so, as you explained thread will return and since join() is just below the starting of thread it should join the main process but its not happening?
Jochen Arndt 10-Mar-16 3:19am    
Again: Don't call join() after starting the thread. It will block your main thread. When creating the thread it is started in the background, your main thread continues operation and reaches the join() which will block.

join() should be used when signalling a thread to terminate. It will then wait until the thread has been terminated.

How do you know that the thread is not returning?
I think it is time for some debugging to see what happens and where execution stops.

Finally you may also catch boost::exception and report the reason so that you know what happened.
Arthur V. Ratz 22-Mar-16 0:47am    
+5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900