⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientthread.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
字号:
/****************************************************************/
/*																*/
/*  CLIENTTHREAD.CPP											*/
/*																*/
/*  Implementation of the Client Thread.						*/
/*	Created when a client logs on to the server and processes	*/
/*  'Send' commando's.											*/
/*																*/
/*	This class is a part of the Mini ASP Web Server				*/
/*																*/
/*  Programmed by Pablo van der Meer							*/
/*	This code is stolen from: http://www.pablovandermeer.nl		*/
/*																*/
/*  Last updated: July 4, 2003									*/
/*																*/
/****************************************************************/

#include "stdafx.h"
#include "Server.h"
#include "ClientThread.h"
#include "ServerDlg.h"

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


IMPLEMENT_DYNCREATE(CClientThread, CWinThread)

CClientThread::CClientThread()
{
	m_hWndOwner = NULL;
}


CClientThread::~CClientThread()
{
}


BEGIN_MESSAGE_MAP(CClientThread, CWinThread)
	//{{AFX_MSG_MAP(CClientThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/********************************************************************/
/*																	*/
/* Function name: InitInstance										*/		
/* Description  : Initialize thread									*/
/*																	*/
/********************************************************************/
BOOL CClientThread::InitInstance()
{
	CoInitialize(NULL);

	// Attach the socket handle to a CSocket object.
	// This makes sure that the socket notifications are sent to this thread.
	m_ClientSocket.Attach(m_hSocket);
	m_ClientSocket.AsyncSelect(FD_READ | FD_CLOSE);
	m_ClientSocket.m_pThread = this;

	UINT nPort;
	m_ClientSocket.GetPeerName(m_strPeerName, nPort);

	// dynamically allocate memory for IP address (receiver will delete it!)
	int nLength = m_strPeerName.GetLength();
	LPSTR lpszPeerName = new char[nLength+1];
	lstrcpy(lpszPeerName, m_strPeerName);
	
	// Post a message to the main thread so that it can update the number of open connections
	::PostMessage(m_hWndOwner, WM_THREADSTART, (WPARAM)lpszPeerName, (LPARAM)m_nThreadID);
	return TRUE;
}


/********************************************************************/
/*																	*/
/* Function name: ExitInstance										*/		
/* Description  : Clean up and leave.								*/
/*																	*/
/********************************************************************/
int CClientThread::ExitInstance()
{
	// delete this thread from the linked list
	CServerDlg *pWnd = (CServerDlg *)AfxGetApp()->m_pMainWnd;
	if (pWnd != NULL)
	{
		pWnd->m_CriticalSection.Lock();

		POSITION pos = pWnd->m_ThreadList.Find(this);
		if(pos != NULL)
		{
			pWnd->m_ThreadList.RemoveAt(pos);
		}
		pWnd->m_CriticalSection.Unlock();

	} 

	// dynamically allocate memory for IP address (receiver will delete it!)
	int nLength = m_strPeerName.GetLength();
	LPSTR lpszPeerName = new char[nLength+1];
	lstrcpy(lpszPeerName, m_strPeerName);
	
	// Post message to the main thread that this socket connection has closed
	::PostMessage(m_hWndOwner, WM_THREADCLOSE, (WPARAM)lpszPeerName, (LPARAM)m_nThreadID);

	CoUninitialize();
	return CWinThread::ExitInstance();
}


/********************************************************************/
/*																	*/
/* Function name: PostStatusMessage									*/		
/* Description  : Post status message.								*/
/*																	*/
/********************************************************************/
void CClientThread::PostStatusMessage(LPCTSTR lpszStatus)
{
	CString strData;
	strData.Format("%s", lpszStatus);

	int nLength = strData.GetLength();

	if (nLength > 255)
	{
		strData = strData.Left(255);
		nLength = strData.GetLength();
	}

	// dynamically allocate memory for status message (receiver will delete it!)
	LPSTR lpszData = new char[nLength+1];
	lstrcpy(lpszData, strData);
	::PostMessage(m_hWndOwner, WM_ADDTRACELINE, (WPARAM)lpszData, (LPARAM)m_nThreadID); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -