QTcpSocket断开自动重新连接 auto connect after disconnected

class Socket
{
	public:
		Socket();
		~Socket();
		public slots:
		void OnConnected();
		void OnDisconnected();
		public:
			QTcpSocket m_tcpSocket;
			bool m_bConnected();
};


Socket:Socket()
{
	m_bConnected = false;
	connect(&m_tcpSocket, SIGNAL(connected()), this, SLOT(OnConnected()));
	connect(&m_tcpSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnected()));
	while(!m_bConnected)
	{
		m_tcpSocket.connectToHost("192.168.1.111", 8741);
		m_tcpSocket.waitForConnected();
		sleep(5);
	}


}


Socket::~Socket()
{


}


void Socket::OnConnected()
{
	m_bConnected = true;
}


void Socket::OnDisconnected()
{
	m_bConnected = false;
	while(!m_bConnected)
	{
		m_tcpSocket.connectToHost("192.168.1.111", 8741);
		m_tcpSocket.waitForConnected();
		sleep(5);
	}
}


你可能感兴趣的:(QTcpSocket断开自动重新连接 auto connect after disconnected)