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

C++ Socket library tutorial

Using the DisplaySocket as a Client

Prev   |   Menu   |   Next

Client implementation

When using a Socket class as a client, the Open() method is used to connect to another host / program. When the connection is established, the OnConnect() method is called by the SocketHandler. When a Socket object is created dynamically and given to the SocketHandler, the method SetDeleteByHandler() is called to make sure the SocketHandler will free the memory used by the Socket when it is dropped from the socket list.

A Socket object is given to the SocketHandler using the method Add(). Socket's added to a handler is not immediately put in the socket list and will not show up in the SocketHandler GetCount() method (which returns the number of sockets in the socket list). Therefore, Select() is called once before checking GetCount().

The while() loop will continue until the SocketHandler deletes the Socket object.

displayclient.cpp
#include "DisplaySocket.h"
#include <SocketHandler.h>


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

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



Prev   |   Menu   |   Next

Valid HTML 4.01!

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