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

📄 ctrl_clnt9doc.cpp

📁 简单的远程控制工具,分为服务器与客户斋,让你了解socket编程的知识.
💻 CPP
字号:
// Ctrl_Clnt9Doc.cpp : implementation of the CCtrl_Clnt9Doc class
//

#include "stdafx.h"
#include "Ctrl_Clnt9.h"

#include "Ctrl_Clnt9Doc.h"
#include "MainFrm.H"

#include "InfoView.h"
#include "DlgSetup.h"
//DEL #include "msg.h"
#include "string.H"
#include "DlgGetPos.h"
#include "..\\CtrlSrvr2\SendKey.H"
#include "SendMsg.H"
extern  int ReadSafe(CWSocket *lpWSK,  char * lpData, int nSize);

#ifdef _WIN32
#ifndef _UNICODE
#include <strstrea.h>
#endif
#endif

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

/////////////////////////////////////////////////////////////////////////////
// CCtrl_Clnt9Doc

IMPLEMENT_DYNCREATE(CCtrl_Clnt9Doc, CDocument)

BEGIN_MESSAGE_MAP(CCtrl_Clnt9Doc, CDocument)
	//{{AFX_MSG_MAP(CCtrl_Clnt9Doc)
	ON_COMMAND(IDM_CONNECT_CONNECT, OnConnectConnect)
	ON_COMMAND(IDM_CONNECT_DISCONNECT, OnConnectDisconnect)
	ON_COMMAND(IDM_PERFORM_GET_SCREEN, OnPerformGetScreen)
	ON_COMMAND(IDM_VIEW_CLEAN, OnViewClean)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



/////////////////////////////////////////////////////////////////////////////
// CCtrl_Clnt9Doc construction/destruction
CCtrl_Clnt9Doc::CCtrl_Clnt9Doc()
{
	// TODO: add one-time construction code here
//DEL    m_bBusy = FALSE;
//DEL	m_lKeySend = 900;
//DEL    m_iSendPoint = 0;
//DEL	m_pSocket = NULL;
//DEL	m_pFile = NULL;
//DEL	m_pArchiveIn = NULL;
//DEL	m_pArchiveOut = NULL;

	m_strServer = _T("127.0.0.1");
	m_nPort = 700;
	m_bConnecting = FALSE;
	m_bUsing = FALSE;
	m_dlgGetScreen = NULL;
    m_bConnectAuto = TRUE;
	m_nCompress = 0;
	m_nJpegQ = 0;
	m_nBits = 0;
	m_nSrvrBits = 0;
	m_nArea = 0;
	m_strPassword = _T("pass");

//Setup Get Image
  	m_hPackData = NULL;
	m_hBmpData = NULL;
  	m_lpPackData = NULL;
	m_lpBmpData = NULL;
    m_dwPackSize = 0;
	m_dwBmpSize = 0;
    m_dwPackMaxSize = 0;
	m_dwBmpMaxSize = 0;
	m_nCellSize = 50;
	m_bDlgScreen = FALSE;
	m_nEdtSecond = 1000;
	m_nVersion = 0;
}

CCtrl_Clnt9Doc::~CCtrl_Clnt9Doc()
{
//DEL	delete m_pArchiveOut;
//DEL	delete m_pArchiveIn;
//DEL	delete m_pSocket;
   m_dlgGetScreen = NULL;
   DisconnectSocket();
}

BOOL CCtrl_Clnt9Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CCtrl_Clnt9Doc serialization

void CCtrl_Clnt9Doc::Serialize(CArchive& ar)
{
	// CEditView contains an edit control which handles all serialization
	((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CCtrl_Clnt9Doc diagnostics

#ifdef _DEBUG
void CCtrl_Clnt9Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CCtrl_Clnt9Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CCtrl_Clnt9Doc commands

//DEL void CCtrl_Clnt9Doc::DeleteContents() 
//DEL {
//DEL 	// TODO: Add your specialized code here and/or call the base class
//DEL 	DisconnectSocket (m_strHandle);
//DEL 
//DEL 	for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
//DEL 	{
//DEL 		CView* pView = GetNextView(pos);
//DEL 	}
//DEL 
//DEL 	CDocument::DeleteContents();
//DEL }

BOOL CCtrl_Clnt9Doc::ConnectSocket()
{
//	int nRet;
	CString info;
//关闭原有连接
    m_bUsing = false;
	g_pDoc = this;

	if (m_bConnecting == TRUE) m_wskClient.Close();
//打开连接
	m_bConnecting = m_wskClient.Connection(m_strServer, m_nPort);
	if(!m_bConnecting)
	{
	    DisplayMsg("MSG:  Can not connect !!! ");
		return FALSE;
	}
    m_wskClient.SetTimeout(60);
//返回首次连接成功
	SENDMSG tagSendMsg;
	if (ReadSafe(&m_wskClient, (char *)&tagSendMsg, sizeof(SENDMSG)) < 0)
        goto Err01;
	DisplayMsg("MSG:  Connected OK!!! ");
//取版本号等信息
/*	if (!SendMsg(CMD_GET_SCREEN_INFO))
		goto Err01;
*/
    memset(&tagSendMsg, 0, sizeof(SENDMSG));
	memcpy(tagSendMsg.szPassword, m_strPassword, 
		   m_strPassword.GetLength());
	tagSendMsg.wCmd = CMD_GET_SCREEN_INFO;
	if (m_wskClient.SendData((char *)&tagSendMsg,sizeof(SENDMSG), 60) < 0)
		goto Err01;
	if (ReadSafe(&m_wskClient, (char *)&tagSendMsg, sizeof(SENDMSG)) < 0)
        goto Err01;
	if (tagSendMsg.nVersion != DEF_VERSION)
		goto Err01;
	m_nBits = tagSendMsg.nBits;      
	m_nSrvrBits = tagSendMsg.nBits;
	m_nVersion = tagSendMsg.nVersion;
	m_SrvrArea = CRect (0, 0, tagSendMsg.rcArea.right, tagSendMsg.rcArea.bottom);
	return TRUE;
Err01:
	DisconnectSocket();
//	info.Format("MSG:  Error version %d!!! ", m_nVersion);
//    DisplayMsg(info);
	return FALSE;
}
//DEL BOOL CCtrl_Clnt9Doc::ConnectImage()
//DEL {
//DEL 	if (m_bImageConnecting)	return true;
//DEL 	m_bImageConnecting = m_wskImage.Connection(m_strServer, m_nPort);
//DEL 	if(!m_bImageConnecting)
//DEL 	{
//DEL 	    DisplayMsg("MSG: Image socket can not connect !!! ");
//DEL 		return FALSE;
//DEL 	}
//DEL /*	int nRet;
//DEL     CString strtmp;
//DEL 	SENDMSG tagSendMsg;
//DEL 	memset(&tagSendMsg,0, sizeof(SENDMSG));
//DEL 	nRet = ReadSafe(&m_wskImage, (char *)&tagSendMsg, sizeof(SENDMSG));
//DEL     if (nRet < 0 || tagSendMsg.iErrorCode < 0) {
//DEL 		m_wskImage.Close();
//DEL 	    DisplayMsg("MSG:  Error in Connected !!! ");
//DEL 		m_bImageConnecting = FALSE;
//DEL 		return FALSE;
//DEL 	}
//DEL 	DisplayMsg("MSG:  Image socket connected OK!!! ");
//DEL 	strtmp = tagSendMsg.szText;
//DEL 	if (strtmp != "V1.0")
//DEL 	{
//DEL 		m_wskImage.Close();
//DEL 	    DisplayMsg("MSG:  Error in version !!! ");
//DEL 		m_bImageConnecting = FALSE;
//DEL 		return FALSE;
//DEL 	}
//DEL */
//DEL 	m_bImageConnecting = true;
//DEL 	return TRUE;
//DEL }

BOOL CCtrl_Clnt9Doc::DisconnectSocket()
{
    BOOL nRet = false;
	if(m_bConnecting)
	{   nRet = true;
		m_wskClient.Close();
		m_bConnecting = false;
	}
//DEL    if(m_bImageConnecting)
//DEL	{   nRet = true;
//DEL		m_wskImage.Close();
//DEL		m_bImageConnecting = false;
//DEL	}
	return nRet;

}




void CCtrl_Clnt9Doc::DisplayMsg(LPCTSTR lpszText)
{
	for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
	{

		CString strHead;
		CString strMsg = lpszText;
		int nHead = strMsg.Find (":",0);
		if (nHead > 0)
			strHead = strMsg.Left (nHead);
		if (strHead == "MSG")
		{
			strMsg = strMsg.Right (strMsg.GetLength ()-nHead-2);
			((CMainFrame *)AfxGetApp()->m_pMainWnd)->m_wndResult.Add_Result_Msg (strMsg);
			return;
		}

		CView* pView = GetNextView(pos);
		CInfoView* pInfoView = DYNAMIC_DOWNCAST(CInfoView, pView);
		if (pInfoView != NULL)
		{
			pInfoView->Display_Message(lpszText);
		}
	}
}

BOOL CCtrl_Clnt9Doc::SaveModified() 
{
	// TODO: Add your specialized code here and/or call the base class

	return TRUE;
//	return CDocument::SaveModified();
}

void CCtrl_Clnt9Doc::OnConnectConnect() 
{
	// TODO: Add your command handler code here
	CDlgSetup Dialog;
    BOOL b_old;
	Dialog.m_strEdtHandle=m_strHandle;
	Dialog.m_strEdtServer=m_strServer;
	Dialog.m_nEdtPort=m_nPort;
	Dialog.m_strEdtPw = m_strPassword;
	Dialog.m_bConnectAuto = m_bConnectAuto;
	b_old = m_bConnectAuto;
	Dialog.Create (this);

	while(TRUE)
	{
		if (IDOK != Dialog.DoModal())	break;
		 if (m_strPassword.GetLength() >= 20) {
		    AfxMessageBox("输入的密码错误!",MB_OK);
			continue;
		 }
         OnConnectDisconnect();
	     if(ConnectSocket()) break;
		 AfxMessageBox("无法连接服务端!");
	}
	m_bConnectAuto = b_old;
	
}

void CCtrl_Clnt9Doc::OnConnectDisconnect() 
{
	// TODO: Add your command handler code here
	if (DisconnectSocket())
	   DisplayMsg("MSG:  It is disconnected !!! ");
}

void CCtrl_Clnt9Doc::OnPerformGetScreen() 
{
	// TODO: Add your command handler code here
//	CDlgGetPos	dlg;
//	m_dlgGetScreen = &dlg;
//	dlg.Create (this);
//	m_bDlgScreen = TRUE;
//	dlg.DoModal();
	m_dlgGetScreen = new CDlgGetPos;
	m_dlgGetScreen->Create(this);
	m_bDlgScreen = TRUE;
	m_dlgGetScreen->DoModal();
	delete m_dlgGetScreen;
	m_dlgGetScreen = NULL;
}

void CCtrl_Clnt9Doc::OnViewClean() 
{
	// TODO: Add your command handler code here
	((CMainFrame *)AfxGetApp()->m_pMainWnd)->m_wndResult.m_ListCtrl.DeleteAllItems ();
	
}


inline int CmdProc_RecFile(char *strFile, HGLOBAL lpFileData)
{
	CFile	fp;
	DWORD	dwFileSize;
	CFileException e;

	int nRC = fp.Open (strFile,CFile::modeCreate | CFile::modeWrite,&e);
	if (nRC == 0)
		return -1;
	else
	{
		dwFileSize = GlobalSize (lpFileData);
		fp.WriteHuge (lpFileData,dwFileSize);
		fp.Close ();
		GlobalFree (lpFileData);
		return 0;
	}
	return 0;
}


/*
void CCtrl_Clnt9Doc::ReceiveMsg()
{
	CMsg msg;
  	HGLOBAL	lpFileData;
	HANDLE hMem;
	if (m_pArchiveIn == NULL) return;

	TRY
	{
		msg.Serialize(*m_pArchiveIn);

		if (msg.m_wCmd == CMD_GET_SCREEN)
		{
            if (msg.m_dwBmpSize==0)  //Error
			{
			m_bBusy = FALSE;
            SendMsg(CMD_KEY_MOUSE_BUFFER);
			}
            if (msg.m_bCompress == TRUE) {
			 lpFileData = msg.m_lpBmpData;
			 hMem = GlobalAlloc (GHND,msg.m_dwFileSize);
			 if (hMem == NULL) return;
			 msg.m_lpBmpData  = (LPSTR)GlobalLock (hMem);
			 if (msg.m_lpBmpData == NULL)
			 {
			     GlobalFree(hMem);
				 return;
			 }   
			 m_Decode.ntDecode((char *)msg.m_lpBmpData,(char *)lpFileData);
			 GlobalUnlock(lpFileData);   
			 GlobalFree(lpFileData);
			}
			m_dlgGetScreen->Get_Screen_Data (msg.m_lpBmpData,msg.m_dwBmpInfoSize);
			m_bBusy = FALSE;
            SendMsg(CMD_KEY_MOUSE_BUFFER);
		}
		if (msg.m_wCmd == CMD_GET_SCREEN_INFO)
		{
			m_nRmtBits = msg.m_nBits;
			m_nRmtWidth = msg.m_rcArea.right;
			m_nRmtHeight = msg.m_rcArea.bottom;
		}
		if (msg.m_wCmd == CMD_FILE_RECEIVE)
		{
			CFile fp;
			int nRC = fp.Open (m_strSaveFile,CFile::modeCreate | CFile::modeWrite);
			if (nRC == 0)
				DisplayMsg ("Save file error!"); 
			fp.WriteHuge (msg.m_lpFileData,msg.m_dwFileSize);
			fp.Close ();
			GlobalFree (msg.m_lpFileData);
			DisplayMsg ("Save file Successfully!"); 
		}
		if (msg.m_wCmd == CMD_STATE_INFO ||
		    msg.m_wCmd == CMD_SOCKET_CLOSE)
		{
		  if (msg.m_strText == "OK")
		  {
		     m_bBusy = FALSE;
          }
//          else
//		  	DisplayMsg (msg.m_strText);
		}
		
	}
	CATCH(CFileException, e)
	{
		msg.m_wCmd = CMD_SOCKET_CLOSE;
//		m_pArchiveIn->Abort();
		delete m_pArchiveIn;
		m_pArchiveIn = NULL;
		delete m_pArchiveOut;
		m_pArchiveOut = NULL;
		delete m_pFile;
		m_pFile = NULL;
		delete m_pSocket;
		m_pSocket = NULL;
		m_bConnecting = FALSE;

		CString strTemp;
		if (strTemp.LoadString(IDS_SERVERRESET))
			DisplayMsg(strTemp);
		if (strTemp.LoadString(IDS_CONNECTIONCLOSED))
			DisplayMsg(strTemp);
	}
	END_CATCH

	if (msg.m_wCmd == CMD_SOCKET_CLOSE)
	{
		delete m_pArchiveIn;
		m_pArchiveIn = NULL;
		delete m_pArchiveOut;
		m_pArchiveOut = NULL;
		delete m_pFile;
		m_pFile = NULL;
		delete m_pSocket;
		m_pSocket = NULL;
		m_bConnecting = FALSE;
	}

}
*/

⌨️ 快捷键说明

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