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

C++ Socket library tutorial

Using the DisplaySocket as a Server

Prev   |   Menu   |   Next

Server implementation

To use a Socket class in a server program, a template class inherited from the Socket class is used. An instance of ListenSocket<class X> can be bound to a listen port using the Bind() method. Incoming connections are accepted by the ListenSocket, and a Socket class of type X is created and automatically added to the SocketHandler.

displayserver.cpp
#include <SocketHandler.h>
#include <ListenSocket.h>

#include "DisplaySocket.h"


static	bool quit = false;

int main()
{
	SocketHandler h;
	ListenSocket<DisplaySocket> l(h);

	if (l.Bind(9001))
	{
		exit(-1);
	}
	h.Add(&l);
	h.Select(1,0);
	while (!quit)
	{
		h.Select(1,0);
	}
}



Prev   |   Menu   |   Next

Valid HTML 4.01!

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