Home  +  Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Sockets~
~New~
~Examples~
~Contact~

C++ Socket library tutorial

Resuming a connection

Prev   |   Menu   |   Next

This is a short example on how to handle lost/failed connections. The ResumeSocket class contains a state flag indicating that the socket is connected, and Reconnect method to create a new instance, copy the socket properties and reopen a connection. The OnConnect method is used to set the connected state flag. OnConnectFailed calls Reconnect to create a new connection attempt (ie a new instance of the same socket class). OnDelete reconnects if the socket is connected (check the state flag). OnDelete will be called after OnConnectFailed, so we need the connected state flag to know if we need to make a reconnect in OnDelete or not. Additions to this example could be some logic controlling if a reconnect should be made or not, after being disconnected.

Socket class header file.

ResumeSocket.h
#ifndef _RESUMESOCKET_H
#define _RESUMESOCKET_H

#include <TcpSocket.h>
#include <ISocketHandler.h>


class ResumeSocket : public TcpSocket
{
public:
	ResumeSocket(ISocketHandler&);
	~ResumeSocket();

	/** When a connect is made, we set the m_b_connected flag */
	void OnConnect();
	/** When the connection attempt fails, we try to reconnect with a new socket instance. 
	    This instance will be deleted, so remember to copy member variables. */
	void OnConnectFailed();
	/** If we are connected (m_b_connected is true), we Reconnect again. */
	void OnDelete();

private:
	ResumeSocket(const ResumeSocket& s) : TcpSocket(s) {} // copy constructor
	ResumeSocket& operator=(const ResumeSocket& ) { return *this; } // assignment operator
	/** Create a new instance and reconnect */
	ResumeSocket *Reconnect();
	bool m_b_connected;
};




#endif // _RESUMESOCKET_H

Socket class implementation.

ResumeSocket.cpp
//#include <stdio.h>

#include "ResumeSocket.h"




ResumeSocket::ResumeSocket(ISocketHandler& h)
:TcpSocket(h)
,m_b_connected(false)
{
	// initial connection timeout setting
	SetConnectTimeout(15);
}


ResumeSocket::~ResumeSocket()
{
}


void ResumeSocket::OnConnect()
{
	m_b_connected = true;
}


ResumeSocket *ResumeSocket::Reconnect()
{
	std::auto_ptr ad = GetClientRemoteAddress();
	ResumeSocket *p = new ResumeSocket(Handler());
	p -> SetDeleteByHandler();
	p -> Open(*ad);
	Handler().Add(p);
	return p;
}


void ResumeSocket::OnConnectFailed()
{
	ResumeSocket *p = Reconnect();
	// modify connection timeout setting
	p -> SetConnectTimeout(5);
}


void ResumeSocket::OnDelete()
{
	if (m_b_connected)
	{
		Reconnect();
	}
}



And finally, the main loop.

resume.cpp
#include "ResumeSocket.h"
#include <SocketHandler.h>


int main()
{
	SocketHandler h;
	ResumeSocket *p = new ResumeSocket(h);

	p -> SetDeleteByHandler();
	p -> Open("localhost", 9002);
	h.Add(p);
	h.Select(1,0);
	while (true)
	{
		h.Select(1,0);
	}
}



Prev   |   Menu   |   Next

Valid HTML 4.01!

Validate
Page, code, and content Copyright (C) 2021 by Anders Hedström