Click here to Skip to main content
15,897,704 members
Articles / Desktop Programming / ATL

Simple, Robust and Expandable Winsock Server for Multiple Clients with Easy to Add New Services

Rate me:
Please Sign up or sign in to vote.
4.93/5 (23 votes)
4 Sep 2006CPOL11 min read 114.4K   1.7K   61  
How to build a simple, robust and easily expandable server for multiple clients
/*
Several wrapper class to automatically
free the resources in the destructor on the exit
from a function. They are common for the
client and the server.

Author Vadim Motorine  
email vad@friendly-ware.com
Copyright (C) 2006 Vadim Motorine 
*/

#pragma once

#ifndef VM_SAEWINSCK_H
#define VM_SAEWINSCK_H
#include "STDAFXCmmn.H"
#include "winsock2.h"
#include "../vmClnt/vmClnt/TaskPasportCr.h"
#include <crtdbg.h>

/* It automatically frees the resources allocated for
the WinSock library*/
class CSafeWSAStrtp
{
	bool isinit;
public:
	CSafeWSAStrtp():isinit(false){;}
// after the call of this function the cleanup is automatic
	void GetProtect(){
		isinit = true;}
	bool IsInit(){return isinit;}
	~CSafeWSAStrtp(){Detach();}
	void Detach(){
		if(isinit){
			WSACleanup();
			isinit = false;
		}
	;}
};

/*
If we define the variable of this class at the
the body  beginning of a function, this
function will be protected by the critical 
section. The critical secion should be initalized
slsewhere and supplied via the constructor.
*/
class CSafeCrctclSctn
{
	LPCRITICAL_SECTION csp;
public:
	CSafeCrctclSctn(LPCRITICAL_SECTION cspi):csp(0){csp = cspi;
	EnterCriticalSection(cspi);}
	~CSafeCrctclSctn(){LeaveCriticalSection(csp);}
//
};

//This class is actually used in the client only.
//The class variable should be defined at the beginning
//of the thread function.  Then the destuctor automatically
//reports about the thread exit.
class CSafeExtThrd
{
	CTaskPasportCr* tskPsprt;//the class to communicate with the thread
public:
	CSafeExtThrd(CTaskPasportCr* tskPsprtI):tskPsprt(tskPsprtI){;}
	~CSafeExtThrd(){
		try{//Though it is improbable,  we may delete tskPsprt before the 
		//the thread stops.
			tskPsprt->SetStopped();
			tskPsprt->SetIsItCnnctd(false);
		}
		catch(...){
		
		}
	}
};

//This class  automatically closes the socket in the destructor
//on the exit from the function
class CSafeSckt
{
	SOCKET sct;
public:
	CSafeSckt():sct(INVALID_SOCKET){;}
//The operator "=" puts the socket under the protection	
	void operator=(SOCKET scti){
		if(sct != INVALID_SOCKET){
			return;}
		sct = scti;}
//The destructor closes the socket		
	~CSafeSckt(){Detach();}
	void Detach(){if(sct != INVALID_SOCKET){
		int err;
		err = ::shutdown(sct,SD_BOTH);//returns 0 if no error
		err = ::closesocket(sct);//returns 0 if no error
		sct = INVALID_SOCKET;}
	;}

};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Russian Federation Russian Federation
I am working in the Russian branch of an international company in Novosibirsk (Russia) as the software engineer ("Software Engineer V") and the leader of the small team.

Now I believe that my level is so high that in C++ and MS Windows the specific "technology" does not important to me. It is probably enough to say that in my last shareware project (Your Voice Reminder, year 2006) I substituted my own clocks instead of the regular Microsoft in the MS Windows taskbar and implemented my own code to record and clear the voice messages including trimming the pause at the end. So now theses clocks may say date and time with your own voice. Also when I made a (limited time) test client / server project for a company to get the job (year 2006), I made the best project (as they estimated it) though I had no previous experience in the client /server programming.

The most large software that I made myself from the scratch had more than 60,000 lines of code and more than 300 C++ classes.

During seven years I was the manager of the successful software project in my own small software company (less than 5 people) where I was also the executive manger. The corresponding software was purchased by many Russian phone companies.

I know English and a little French.

I worked in USA (H1b visa only) and have the valid USA social security number.

Before 1993 I was a scientist and have Ph.D. in physics and mathematics.

After 1993 when the salary of the scientists in Russia dropped below the survival level (less than $100 /months) I became the Software Engineer.

Worked across the whole software -- in virtually all aspects of software design and implementation. I also worked with cross functional teams and as the project manager.

Besides C++ I also developed in Java.

Comments and Discussions