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

C++ Socket library tutorial

Using the custom SocketHandler in a server

Prev   |   Menu   |   Next

Server implementation

This is the server implemented using the StatusHandler class. Also note the additional code in the Select() loop. Every 10 seconds calls to the StatusHandler methods Update() and Disconnect() are made. Update() sends status to all connected StatusSocket's, Disconnect() disconnects a StatusSocket after 60 seconds connect time.

customserver.cpp
#include <ListenSocket.h>

#include "StatusHandler.h"
#include "StatusSocket.h"


static	bool quit = false;

int main()
{
	StatusHandler h;
	ListenSocket<StatusSocket> l(h);

	if (l.Bind(9003))
	{
		exit(-1);
	}
	h.Add(&l);
	h.Select(1,0);
	time_t t = time(NULL);
	while (!quit)
	{
		h.Select(1,0);
		if (time(NULL) - t > 10)
		{
			t = time(NULL);
			h.Update();
			h.Disconnect();
		}
	}
}



Prev   |   Menu   |   Next

Valid HTML 4.01!

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