Click here to Skip to main content
15,895,740 members
Articles / Desktop Programming / MFC

Multithreaded Non-blocking Socket Server and Client, Based on Synchronized Socket

Rate me:
Please Sign up or sign in to vote.
3.75/5 (4 votes)
11 Mar 2002 98.1K   3.9K   33  
Non-blocking socket class using synchronized socket.
// SyncClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "SyncClient.h"
#include "process.h"

#include "TestClient.h"
#include "..\src\SyncSocketClient.h"

#include <list>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;


long g_ConnectionsCount = 0;
HANDLE g_hEventStop = NULL;
CRITICAL_SECTION g_cs;
void Start();
void __cdecl TestThread(void*);

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		g_hEventStop = CreateEvent(NULL, TRUE, FALSE, NULL);
		InitializeCriticalSection(&g_cs);
		do
		{
			cout << "Client===============" << endl;
			cout << "0: quit" << endl;
			cout << "1: start" << endl;
			cout << "2: stop" << endl;
			cout << "3: count" << endl;
			cout << "Enter choice:";
			cin >> nRetCode;
			switch(nRetCode)
			{
			case 1:
				Start();
				cout << endl << "client started." << endl << endl;
				break;
			case 0:
			case 2:
				SetEvent(g_hEventStop);
				cout << endl << "client stopped." << endl << endl;
				
				break;
			case 3:
				cout << InterlockedIncrement(&g_ConnectionsCount) - 1 << endl;
				InterlockedDecrement(&g_ConnectionsCount);
				break;
			default:
				break;
			}
		}while(nRetCode != 0);

		CloseHandle(g_hEventStop);
		DeleteCriticalSection(&g_cs);

		cout << "quit" << endl;
	}

	
	return nRetCode;
}

void Start()
{
	ResetEvent(g_hEventStop);
	for(int i = 0; i < 1; i++)
		_beginthread(TestThread, 0, 0);
}

void __cdecl TestThread(void*)
{
	list<CSyncSocketClient<CTestClient>*> listClients;
	
	CSyncSocketClient<CTestClient> * pClient = NULL;
	for(int i = 0; i < 1; i++)
	{
		if(WAIT_OBJECT_0 ==WaitForSingleObject(g_hEventStop, 5))
			break;

		if(pClient = new CSyncSocketClient<CTestClient>())
		{
			if(pClient->Connect("127.0.0.1", 586))
				InterlockedIncrement(&g_ConnectionsCount);

			listClients.push_back(pClient);			
		}
	}

	Sleep(60000 * 10);  // how long should you wait? i don't know
	while(listClients.size())
	{
		pClient = *listClients.begin();
		listClients.pop_front();

		if(pClient)
			delete pClient;
	}
}


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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions