Click here to Skip to main content
15,881,715 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 97.8K   3.9K   33  
Non-blocking socket class using synchronized socket.
// TestClient.cpp: implementation of the CTestClient class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SyncClient.h"
#include "TestClient.h"
#include "iostream"
using namespace std;

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTestClient::CTestClient(SOCKET s, const char* pszPeerAddr, unsigned short nPeerPort, BOOL bCloseWait /* = TRUE */ )
 : CSyncSocketWorker<CTestClient>(s, pszPeerAddr, nPeerPort, bCloseWait)
{
	
}

CTestClient::~CTestClient()
{
}

extern CRITICAL_SECTION g_cs;

void CTestClient::ProcessData()
{
 	const int nBufferSize = 8192;
	char szBuffer[nBufferSize];

	CFile file;
	file.Open(TEXT("recv.jpg"), CFile::modeCreate | CFile::modeWrite);
	int nRead;

	DWORD dwStart = GetTickCount();
	while(nRead = Read(szBuffer, nBufferSize))
	{
		file.Write(szBuffer, nRead);
		if(IsShutingdown())
		{
			cout << "shuting down" << endl;
			break;
		}
	}

	cout << "Speed = " << double(file.GetLength()) / (double(GetTickCount() - dwStart) / 1000.0) / 1024 << "KB/s" << endl;
	file.Close();
}

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