Click here to Skip to main content
15,897,718 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
/*
CvmClntDlg : implementation file

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



//

#include "stdafx.h"
#include "vmClnt.h"
#include "vmClntDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() 
: CDialog(CAboutDlg::IDD)
{
	
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CvmClntDlg dialog




CvmClntDlg::CvmClntDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CvmClntDlg::IDD, pParent)
	, SrvrIP(_T(""))
	, srvrPort(0)
	, fldrPth(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
//Stores this dialog  pointer in the main working class CvmClntHlpr	
	StWnPrntPtr(this);
}

void CvmClntDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_SRVRIP_ED, SrvrIP);
	DDX_Text(pDX, IDC_SRVR_PRT, srvrPort);
	DDX_Text(pDX, IDC_EDIT4, fldrPth);
}

BEGIN_MESSAGE_MAP(CvmClntDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_SRVRTM, &CvmClntDlg::OnBnClickedBtnSrvrtm)
	ON_BN_CLICKED(IDC_GTFLSANDSZS, &CvmClntDlg::OnBnClickedGtflsandszs)
	ON_BN_CLICKED(IDC_DSCNNCT_BTN, &CvmClntDlg::OnBnClickedDscnnctBtn)
	ON_BN_CLICKED(IDCANCEL, &CvmClntDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


// CvmClntDlg message handlers

BOOL CvmClntDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	srvrPort = GetDfltPrt();// to show the default port
	UpdateData(false);
	
//Getting the status window handle	
	CEdit* psstsP = reinterpret_cast<CEdit*>(GetDlgItem(IDC_STATUS_ED));	
	
//Stores the status window handle to use in the application	as well as
//this dialog wondow handle
	SetHWND(psstsP->m_hWnd, this->m_hWnd);//to send the status messages from the thread
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CvmClntDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CvmClntDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CvmClntDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//Preiminary check of the user input
bool CvmClntDlg::Check()
{
	UpdateData(true);
	SrvrIP.Trim();//IDC_SRVR_PRT
	if(SrvrIP.IsEmpty()){
		AfxMessageBox("Please, enter the server name or IP address");
		UpdateData(false);
		return false;		
	}
	CEdit* prtIdEP = reinterpret_cast<CEdit*>(GetDlgItem(IDC_SRVR_PRT));
	CString prtStr;
	prtIdEP->GetWindowText(prtStr);
	prtStr.Trim();
	if(prtStr.IsEmpty()){
		AfxMessageBox("Please, enter the port");
		UpdateData(false);
		return false;			
	}
	return true;
}

//Asks the server time
void CvmClntDlg::OnBnClickedBtnSrvrtm()
{
	if(!Check())
		return;
	DoCmmnd(CVMprotocolCr::TIME, SrvrIP.GetString(), srvrPort, " ");
}

//Asks the server for the list 
//of all the subdirectories of the given
//server directory together with all files
//and their sizes
void CvmClntDlg::OnBnClickedGtflsandszs()
{
	if(!Check())
		return;
	fldrPth.Trim();
	if(fldrPth.IsEmpty()){
		AfxMessageBox("Please enter the file name or the folder path");
		return;
	}	
	DoCmmnd(CVMprotocolCr::FILE_SZS, SrvrIP.GetString(), srvrPort, fldrPth.GetString());
}

//Diconnects
void CvmClntDlg::OnBnClickedDscnnctBtn()
{
	DoCmmnd(CVMprotocolCr::BY_BY, SrvrIP.GetString(), srvrPort, " ");
}

// On cancel  or "Esc"
void CvmClntDlg::OnBnClickedCancel()
{
	
	OnCancel();
}

void CvmClntDlg::OnCancel()
{
//First we disconnect
	DoCmmnd(CVMprotocolCr::BY_BY, SrvrIP.GetString(), srvrPort, " ");

	CDialog::OnCancel();
}

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